Source of scope3.cpp


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

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

  8: int i, j;      //Global variables

 10: void P(int& i) //Parameter i is local to P
 11: {
 12:     int j;    //Local to P

 14:     j = 6;
 15:     cout << i << j << endl;  //Which "i" and "j"
 16:     i = i + j;               //are being used here?
 17:     cout << i << j << endl;
 18: }

 20: int main()
 21: {
 22:     cout << endl;
 23:     i = 3;    //Which "i" and "j"
 24:     j = 4;    //are being used here?
 25:     cout << i << j << endl;
 26:     P(j);
 27:     cout << i << j << endl;
 28: }