COSC 181 Foundations of Computer Programming Class 24 6 10 Scope Rules Cont File scope For an identifier declared outside any function or class Such an identifier is known in all functions from the point at which it is declared until the end of the file Global variables function definitions and function prototypes placed outside a function all have file scope Function scope Labels identifiers followed by a colon such as start are the only identifiers with function scope Can be used anywhere in the function in which they appear Cannot be referenced outside the function body Labels are implementation details that functions hide from one another 2 6 10 Scope Rules Cont Block scope Identifiers declared inside a block have block scope Block scope begins at the identifier s declaration Block scope ends at the terminating right brace of the block in which the identifier is declared Local variables and function parameters have block scope The function body is their block Any block can contain variable declarations Identifiers in an outer block can be hidden when a nested block has a local identifier with the same name Local variables declared static still have block scope even though they exist from the time the program begins execution Storage duration does not affect the scope of an identifier 3 6 10 Scope Rules Cont Function prototype scope Only identifiers used in the parameter list of a function prototype have function prototype scope Parameter names appearing in a function prototype are ignored by the compiler Identifiers used in a function prototype can be reused elsewhere in the program without ambiguity However in a single prototype a particular identifier can be used only once 4 Common Programming Error 6 12 Accidentally using the same name for an identifier in an inner block that is used for an identifier in an outer block when in fact the programmer wants the identifier in the outer block to be active for the duration of the inner block is normally a logic error 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Fig 6 12 f ig06 12 cpp A scoping example inc lude iostream us ing std cout us ing std endl Outline fig06 1 vo id useLocal void function prototype void useStaticLocal void function prototype void useGlobal void function prototype 2 cpp int x 1 global variable int main int x 5 local variable to main Declaring a global variable outside any class or function definition Local cout loca l x in main s outer scope isx endl 1 of 4 variable x that hides global variable x start new scope int x 7 hides x in outer scope Local variable x in a block that local variable x in outer scope cout loca l x in main s inner scope is x endl hides end new scope cout loca l x in main s outer scope isx endl 6 26 27 useLocal useLocal has local x 28 useStaticLocal useStaticLocal has static local x 29 useGlobal useGlobal uses global x 30 useLocal useLocal reinitializes its local x 31 useStaticLocal static local x retains its prior value 32 useGlobal global x also retains its value 33 34 35 cout nlocal x in main is x endl return0 indicates successful termination Outline fig06 1 2 cpp 2 36 end main 37 of 4 38 useLocal reinit ia l iz es local variable x during each call 39 vo id useLocal void 40 41 Local variable that gets recreated and reinitialized each time useLocal is called int x 25 initialized each time useLocal is called 42 43 cout nlocal x is x on entering useLocal endl 44 x 45 cout local x is x on exiting useLocal endl 46 end function useLocal 7 47 48 useStat ic Loca l in it ia l i z e s 49 f ir s t stat ic t ime the funct ion is ca lled 50 between ca l ls loca l var iab le x only the va lue of x is saved to th is funct ion static local variable that gets initialized only once 51 vo id useStaticLocal void 52 53 static int x 50 initialized first time useStaticLocal is called 54 55 56 cout n lo ca l stat ic x is x on entering useStaticLocal endl 57 x 58 cout local static x is x on exiting useStaticLocal 59 Outline fig06 1 2 cpp 3 of 4 endl 60 end function useStaticLocal 61 62 useGlobal modifies global variable x during each call 63 void useGlobal void Statement refers to global variable x because no local variable named x exists 64 65 cout nglobal x is x on entering useGlobal endl 66 x 10 67 cout global x is x on exiting useGlobal endl 68 end function useGlobal 8 local x in main s outer scope is 5 local x in main s inner scope is 7 local x in main s outer scope is 5 local x is 25 on entering useLocal local x is 26 on ex it ing useLocal local stat ic x is 50 on entering useStaticLocal local stat ic x is 51 on ex it ing useStaticLocal global x is 1 on entering useGlobal global x is 10 on ex it ing useGlobal local x is 25 on entering useLocal local x is 26 on ex it ing useLocal Outline fig06 1 2 cpp 4 of 4 local stat ic x is 51 on entering useStaticLocal local stat ic x is 52 on ex it ing useStaticLocal global x is 10 on entering useGlobal global x is 100 on ex it ing useGlobal local x in main is 5 9 6 11 Function Call Stack and Activation Records Data structure collection of related data items Stack data structure Analogous to a pile of dishes When a dish is placed on the pile it is normally placed at the top Referred to as pushing the dish onto the stack Similarly when a dish is removed from the pile it is normally removed from the top Referred to as popping the dish off the stack A last in first out LIFO data structure The last item pushed inserted on the stack is the first item popped removed from the stack 10 6 11 Function Call Stack and Activation Records Cont Function Call Stack Sometimes called the program execution stack Supports the function call return mechanism Each time a function calls another function a stack frame also known as an activation record is pushed onto the stack Maintains the return address that the called function needs to return to the calling function Contains automatic variables parameters and any local variables the function declares 11 6 11 Function Call Stack and Activation Records Cont Function Call Stack Cont When the called function returns Stack frame for the function call is popped Control transfers to the return address in the popped stack frame If a function makes a call to another function Stack frame for the new function call is simply pushed onto the call stack Return address required by …
View Full Document