COM 1100 Fall 2000 -- Prof. Futrelle -- Lab 5 Directions

Scope of Names -- Sec. 3.6 of Friedman and Koffman

Lab date: Wednesday October 25th. Assignment due by 2pm Friday, October 27th. These directions were posted Sunday, October 22nd.

Goal of the Lab #5:

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:

  1. A global variable can be assigned and used in any function and all references are to the same variable.
  2. Two variables of the same name in different functions: Demonstrate that they can be changed independently. Changing one does not change the other.
  3. The formal parameter name of a function can be the same or different from a variable used when the function is actually called.
  4. A formal parameter in a function definition with the same name as a global will interfere with, "hide" the global, but only within that function.
  5. If you redeclare a variable in a functio with the same name as a global, the global is no longer accessible in your function . It is "hidden". (this is not ordinarily a good idea, since it can be confusing).
  6. Introducing a variable declaration inside braces {...} in a function creates a new scope, independent of the scope before or after the braces. Demonstrate this by introducing and manipulating a variable in braces and showing the value of one of the same name declared outside the braces. Show that outside value before and after the braces. It should be unaffected by what goes on inside the braces.
  7. Show that a declaration inside braces only interferes with globals within the braces.
  8. Plus any other useful examples you can think of that illustrate the nature of scope in C++.