Source of scope1.cpp


  1: //scope1.cpp
  2: //A nonsense program to test your understanding
  3: //of variable scope and lifetime.

  5: #include <iostream>
  6: using namespace std;

  8: void DoSomething(int& x);

 10: int a = 7;  //Global variables (declared outside any function)
 11: int b = 8;
 12: int c = 9;

 14: int main()
 15: {
 16:     cout << endl;
 17:     cout << a << b << c << endl;
 18:     DoSomething(a);
 19:     cout << a << b << c << endl;
 20: }

 22: void DoSomething(int& x)
 23: {
 24:     int b;  //Local variable (with the same name as a global variable)

 26:     a = 1;
 27:     b = 2;  //Which "b" is this, the local or the global?
 28:     x = 3;
 29:     c = 4;
 30:     cout << a << b << c << endl;
 31: }