Wednesday 25 June 2014

Local Variables and Global Variables in C++


Local Variables and Global Variables



The Basic Difference Between the Local Variables and Global Variables is


Local Variables

1.  It is basically a variable which is declared with in a function or inside the block.

2. It is basically accessible only within a function or within a block in which it is declared .


Global Variables

1. It is basically a variable declared outside all the functions.

2. It is accessible throughout the program .


EXAMPLE :




HERE IN THIS EXAMPLE VAL IS A GLOBAL VARIABLE AND MARKS IS A LOCAL VARIABLE.

#include<iostream.h>
float val =90;
void LOCAL (int t)
{
int marks=0;
for(int i = 0 , i<t ; i++)
{
marks + = i ;
}
cout<<val+marks;
}
void main()
{
LOCAL(40);
}




 

No comments:

Post a Comment