Variable Scope, global variable, local variable - C++ Function

C++ examples for Function:Global Variable

Description

Variable Scope, global variable, local variable

Demo Code

#include <iostream>
using namespace std;
int firstnum;    // create a global variable named firstnum
void valfun();   // function prototype (declaration)
int main()/*from ww  w  .  j  a va 2s  . co m*/
{
   int secnum;      // create a local variable named secnum
   firstnum = 10;   // store a value in the global variable
   secnum = 20;     // store a value in the local variable
   cout << "From main(): firstnum = " << firstnum << endl;
   cout << "From main(): secnum = " << secnum << endl;
   valfun();        // call the function valfun
   cout << "\nFrom main() again: firstnum = " << firstnum << endl;
   cout << "From main() again: secnum = " << secnum << endl;
   return 0;
}
void valfun()
{
   int secnum;
   secnum = 30;  // affects only this local variable's value
   cout << "\nFrom valfun(): firstnum = " << firstnum << endl;
   cout << "From valfun(): secnum = " << secnum << endl;
   firstnum = 40;    // changes firstnum for both functions
   return;
}

Result


Related Tutorials