Unformatted text preview:

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 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 another36.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 identifier46.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 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;Outlinefig06_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 useLocalOutlinefig06_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 useGlobalOutlinefig06_12.cpp (3 of 4)static local variable that gets initialized only onceStatement refers to global variable x because no local variable named x exists9Outlinefig06_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 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 stack116.11


View Full Document

UVa-Wise COSC 181 - Foundations of Computer Programming

Download Foundations of Computer Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Foundations of Computer Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Foundations of Computer Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?