Global variable
Global variables are variables that defined outside the major function block. These variables are referred by the similar data type and through the similar name throughout the program in the calling portion of a program and in the function block both . Whenever a few of the variables are treated as constants in both the main and the function block that is advisable to use global variable.
Example:
int x,y = 4;
void main(void)
{
void funct1( );
x = 10;
funct1 ( );
}
void funct1 ( )
{
int sum;
sum = x +y;
}
Sample program
//global and local variable declaration
# include<iostream.h>
int x; //global variable
int y = 5; //global variable
void main (void)
{
x = 10;
void sum (void);
sum ( );
}
void sum ( )
{
int sum; //local variable
Sum = x + y;
cout<< "x = " <<x<<endl;
cout<<"y="<<y<<endl;
cout<<"sum = "<<sum << endl;
}
Output of the program
x = 10 y = 5
Sum = 15