#include <iostream.h>
/********************************************************
An example to demonstrate the scope
of variables
********************************************************/
void fn(char , long );
int z = 25;
main(){
long x = 100;
float y = 6.2897;
char c = 'a';
double z = 3.145;
cout << "Inside main\n";
cout << "c = " << c << "\n";
cout << "x = " << x << "\n";
cout << "y = " << y << "\n";
cout << "z = " << z << "\n";
cout << "::z = " << ::z << "\n\n"; // Access global z
fn(c, x);
cout << "Back inside main\n";
cout << "The value of x is still " << x << "\n";
}
void fn(char c, long x){
double y = 3.1459;
++x;
cout << "Inside fn\n";
cout << "x = " << x << "\n";
cout << "z = " << z << "\n";
cout << "y = " << y << "\n\n";
{
char y = c;
cout << "Inside block inside fn\n";
cout << "y = " << y << "\n\n";
}
y/=3.00;
cout << "Outside block inside fn\n";
cout << "y = " << y << "\n";
cout << "z++ = " << z++ << ", and ++z = " << ++z << "\n\n";
}
Output:
Inside main
c = a
x = 100
y = 6.2897
z = 3.145
::z = 25
Inside fn
x = 101
z = 25
y = 3.1459
Inside block inside fn
y = a
Outside block inside fn
y = 1.04863
z++ = 25, and ++z = 27
Back inside main
The value of x is still 100
Back to Previous Page
Document:
Local Date:
Last Modified On: