/* testfunc07.cpp CIS 150 6/9/2005 David Klick This program demonstrates variable scope. */ #include using std::cout; void changeXYZ(int n); // function prototype int w = 3; // global variable int y = 5; // global variable int main(void) { int x = 7; // seen only within main int y = 9; // seen only within main; hides global y int z = 11; // seen only within main // see what values are before function call cout << "Before calling changeXYZ: w=" << w << ", x=" << x << ", y=" << y << ", z=" << z << '\n'; changeXYZ(z); // try to change values // see what values are after function call cout << "After calling changeXYZ: w=" << w << ", x=" << x << ", y=" << y << ", z=" << z << '\n'; return 0; } /* Try to change x, y, and z values. */ void changeXYZ(int z) { int x = 2; // must declare since main's x can't be seen here y = 4; // changes global y z = 6; // tries to change value of z cout << "Inside changeXYZ: w=" << w << ", x=" << x << ", y=" << y << ", z=" << z << '\n'; } /* Sample output: Before calling changeXYZ: w=3, x=7, y=9, z=11 Inside changeXYZ: w=3, x=2, y=4, z=6 After calling changeXYZ: w=3, x=7, y=9, z=11 */