Variable Scope Example : Variable Scope « Function « C++






Variable Scope Example

Variable Scope Example
 


#include <iostream>

using namespace std;

int subtract (int a, int b);

int global = 5;  

int main(void)
{

     int a, b;

     a = 5;

     b = 3;

     cout << "The value of main's a is: " << a << endl
          << "The value of main's b is: " << b << endl
          << "The value of global is: " << global << endl;
     global = 2 + subtract(a,b);
     cout << "The value of main's a now is: " << a << endl
          << "The value of global now is: " << global << endl;
     return 0;
}  

int subtract(int a, int b)
{
     cout << "The value of subtract's a is: " << a << endl
          << "The value of subtract's b is: " << b << endl;
     a = a - b + global;
     return a;
}


           
         
  








Related examples in the same category

1.Variables: Global, Local variableVariables: Global, Local variable
2.Function with global value
3.The Scope Resolution Operator
4.Effect of scope on automatic variables
5.global variables