Programming in c++ Scope, Lifetime, and More on Functions Dale/eems/Headington
1 Scope, Lifetime, and More on Functions
Programming in C++ Chapter 8 Topics s Local Scope vS. Global Scope of an Identifier Detailed Scope Rules to Determine which Variables are Accessible in a block Determining the Lifetime of a Variable Initializations in Declarations s Interface Design %Writing a Value-Returning Function for a Task Some Value-Returning Functions with Prototypes in Header Files ectype and cmath Creating and Using a Module Structure Chart Stub Testing a Program
2 Chapter 8 Topics ❖Local Scope vs. Global Scope of an Identifier ❖Detailed Scope Rules to Determine which Variables are Accessible in a Block ❖Determining the Lifetime of a Variable ❖Initializations in Declarations ❖Interface Design ❖Writing a Value-Returning Function for a Task ❖Some Value-Returning Functions with Prototypes in Header Files cctype and cmath ❖Creating and Using a Module Structure Chart ❖Stub Testing a Program
Programming in C++ Scope of Identifier o the scope of an identifier (or named constant) means the region of program code where it is legal to use that identifier for any purpose
3 Scope of Identifier ❖the scope of an identifier (or named constant) means the region of program code where it is legal to use that identifier for any purpose
Programming in C++ Local Scope VS. Global cope o the scope of an ☆ the scope of ar identifier that is identifier that is declared inside a declared outside of block(this includes all namespaces, function parameters) functions and classes extends from the extends from point of point of declaration declaration to the end to the end of the of the entire file block containing program code
4 Local Scope vs. Global Scope ❖the scope of an identifier that is declared inside a block (this includes function parameters) extends from the point of declaration to the end of the block ❖the scope of an identifier that is declared outside of all namespaces, functions and classes extends from point of declaration to the end of the entire file containing program code
Programming in C++ const float TAX RATE =0.05; / global constant float tipRate ∥ global variable void handle( int, float ) ∥ function prototype using namespace std int main( int age //age and bill local to this block float bill i ma b, and tax cannot be used here //TAX RATE and tip Rate can be used handle(age, bill); return 0; void handle(int a, float b) float tax m/a b and tax local to this block l/ age and bill cannot be used here //TAX RATE and tip Rate can be used 5
5 const float TAX_RATE = 0.05 ; // global constant float tipRate ; // global variable void handle ( int, float ) ; // function prototype using namespace std ; int main ( ) { int age ; // age and bill local to this block float bill ; . // a, b, and tax cannot be used here . // TAX_RATE and tipRate can be used handle (age, bill) ; return 0 ; } void handle (int a, float b) { float tax ; // a, b, and tax local to this block . // age and bill cannot be used here . // TAX_RATE and tipRate can be used }