Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17COSC 181 – Foundations of Computer ProgrammingClass 2426.10 Scope Rules (Cont.)File scopeFor 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 fileGlobal variables, function definitions and function prototypes placed outside a function all have file scopeFunction scopeLabels (identifiers followed by a colon such as start:) are the only identifiers with function scopeCan be used anywhere in the function in which they appearCannot be referenced outside the function bodyLabels are implementation details that functions hide from one another36.10 Scope Rules (Cont.)Block scopeIdentifiers declared inside a block have block scopeBlock scope begins at the identifier’s declarationBlock scope ends at the terminating right brace (}) of the block in which the identifier is declared Local variables and function parameters have block scopeThe function body is their block Any block can contain variable declarationsIdentifiers in an outer block can be “hidden” when a nested block has a local identifier with the same nameLocal variables declared static still have block scope, even though they exist from the time the program begins executionStorage duration does not affect the scope of an identifier46.10 Scope Rules (Cont.)Function-prototype scopeOnly identifiers used in the parameter list of a function prototype have function-prototype scopeParameter names appearing in a function prototype are ignored by the compilerIdentifiers used in a function prototype can be reused elsewhere in the program without ambiguityHowever, in a single prototype, a particular identifier can be used only once5Common 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.61 // Fig. 6.12: f ig06_12.cpp2 / / A scoping example.3 #include <iostream>4 us ing std::cout;5 us ing std::endl;67 void useLocal( void ); // function prototype8 void useStaticLocal( void ); // function prototype9 void useGlobal( void ); // function prototype1011 int x = 1; // global variable1213 int main()14 {15 int x = 5; // local variable to main1617 cout << "loca l x in main's outer scope is " << x << endl;18 19 { // start new scope 20 int x = 7; // hides x in outer scope 21 22 cout << "loca l x in main's inner scope is " << x << endl;23 } // end new scope 24 25 cout << "loca l x in main's outer scope is " << x << endl;Outlinefig06_12.cpp (1 of 4)Declaring a global variable outside any class or function definitionLocal variable x that hides global variable xLocal variable x in a block that hides local variable x in outer scope72627 useLocal() ; // useLocal has local x28 useStaticLocal() ; // useStaticLocal has static local x29 useGlobal() ; // useGlobal uses global x30 useLocal() ; // useLocal reinitializes its local x31 useStaticLocal() ; // static local x retains its prior value32 useGlobal() ; // global x also retains its value3334 cout << "\nlocal x in main is " << x << endl;35 return 0; // indicates successful termination36 } // end main3738 // useLocal reinit ia l iz es local variable x during each call39 void useLocal( void )40 {41 int x = 25; // initialized each time useLocal is called4243 cout << "\nlocal x is " << x << " on entering useLocal" << endl;44 x++;45 cout << "local x is " << x << " on exiting useLocal" << endl;46 } // end function useLocalOutlinefig06_12.cpp(2 of 4)Local variable that gets recreated and reinitialized each time useLocal is called84748 / / useStat ic Loca l in it ia l i z e s stat ic loca l var iab le x only the49 / / f ir s t t ime the funct ion is called; value of x is saved50 / / between calls to th is funct ion51 vo id useStaticLocal( void )52 {53 static int x = 50; // initialized first time useStaticLocal is called5455 cout << "\n loca l stat ic x is " << x << " on entering useStaticLocal" 56 << endl;57 x++; 58 cout << "local static x is " << x << " on exiting useStaticLocal" 59 << endl;60 } // end function useStaticLocal6162 // useGlobal modifies global variable x during each call63 void useGlobal( void )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 useGlobalOutlinefig06_12.cpp (3 of 4)static local variable that gets initialized only onceStatement refers to global variable x because no local variable named x exists9Outlinefig06_12.cpp (4 of 4)local x in main's outer scope is 5local x in main's inner scope is 7local x in main's outer scope is 5local x is 25 on entering useLocallocal x is 26 on ex iting useLocallocal static x is 50 on entering useStaticLocallocal static x is 51 on ex it ing useStaticLocalglobal x is 1 on entering useGlobalglobal x is 10 on ex iting useGloballocal x is 25 on entering useLocallocal x is 26 on ex iting useLocallocal static x is 51 on entering useStaticLocallocal static x is 52 on ex it ing useStaticLocalglobal x is 10 on entering useGlobalglobal x is 100 on ex it ing useGloballocal x in main is 5106.11 Function Call Stack and Activation RecordsData structure: collection of related data itemsStack data structureAnalogous to a pile of dishesWhen a dish is placed on the pile, it is normally placed at the topReferred to as pushing the dish onto the stack Similarly, when a dish is removed from the pile, it is normally removed from the topReferred to as popping the dish off the stack A last-in, first-out (LIFO) data structureThe last item pushed (inserted) on the stack is the first item popped (removed) from the stack116.11
View Full Document