Lab date: Wednesday October 25th. Assignment due by 2pm Friday, October 27th. These directions were posted Sunday, October 22nd.
To understand the scope of names. The scope of an identifier name, a variable, or a constant or a formal parameter of a function, is the portion of the program in which a particular instance of a name is "visible". For example, if a particular variable declaration is "in scope" at a certain point, it can be used and changed there and will affect any other use of the variable in the same scope.
Many students are having problems with scope, so a small lab to explore how it works is useful to have at this point in the course. This is a short exercise and I'm sure many of you will be able to finish it quickly, especially if you do some planning in advance.Here is code snippet for a simple example illustrating some aspects of how scoping works in C++:
void fn(); // function prototype, its declaration int aa; // aa is a "global" whose scope is the entire program. void main() { int bb; // the scope of this bb is entirely within main() aa = 4; bb = 5; fn(); cout << "aa was changed in fn() and in main() is now: " << aa << endl; cout << "but the bb in main is still: " << bb << endl; } // the function definition void fn() { int bb; // this is a bb with scope only inside fn() and unrelated to bb in main() bb = 6; // must be set since the bb = 5 in main() has no effect here. cout << "aa is: " << aa << " and bb is: " << bb << endl; aa = 7; // will be "seen" in main() after this returns } /* The output of the program above is: aa is: 4 and bb is: 6 aa was changed in fn() and in main() is now: 7 but the bb in main is still: 5 */
What you are to do for Lab #5:
Create a program that includes a few functions with both global and local variable declarations. Your program should demonstrate the following points: