Local variables
Any variable which has been declared inside a function is called its local variable. The name local denotes that the variable is accessible from within the function it which it is declared. It cannot be used from outside the function. Local variables are also called “private” variables since they are kind of “private property” of a function. We can declare many variables with the same name provided that all of them are declared in different functions.
Example 1 : Working of a local variable
void main()
{
int local=10;
void display();
display();
printf(“\nvalue is %d”,local);
}
void display()
{
int local=20;
printf(“\nvalue is %d”,local);
}
Output
value is 20
value is 10
Description
As you can see we have declared a variable named “local” both in the main() and in display(). The “local” variable declared inside main() has the value 10 while the one declared in display() as the value 20. When the function is called the value 20 is printed first and then 10 is printed. It is quite clear that both the functions are displaying their own value of the variable.
Example 2 : Understanding the function of a local variable
void main()
{
void calculate();
int data;
data=1000;
calculate();
printf(“\nIn main data is %d”,data);
}
void calculate()
{
int data;
data=2000;
printf(“\nIn calculate data is %d”,data);
data+=1000;
}
Output
In calculate data is 2000
In main data is 1000
Description
There are two local variables named “data” each in main () and data(). Before printing the value of data of main, the function is called. Inside calculate () there is another variable with the name “data” which has a value of 2000. After printing 2000 the value is incremented by 1000 making it 3000 but since there is no printf() after it, we won’t see 3000 printed on the screen. After returning to main() , the value of data which was available in main() which is 1000 is printed. The increment has no effect in the main since there are two “data” variables. The “data” of calculate() will be incremented and not the one declared in main()
Example 3 : Local variables and multiple functions
void main()
{
void fun1();
int value=1;
fun1();
value=value+1;
printf(“\nIn main value is %d”,value);
}
void fun1()
{
int value=2;
void fun2();
fun2(); //calling fun2
value=value-1;
printf(“\nIn fun 1 value is %d”,value);
}
void fun2()
{
void fun3();
int value=3;
printf(“\nIn fun2 value is %d”,value);
fun3(); //calling fun3
}
void fun3()
{
int value=4;
printf(“\nIn fun 3 value is %d”,value);
value=value+1;
}
Output
In fun2 value is 3
In fun 3 value is 4
In fun 1 value is 1
In main value is 2
Description
The above program has four function (main(),fun1(),fun2() and fun3()) . All of these functions have a declaration of the variable “value”. Since each function has declared a variable with the same name, there are four different variables with the same name “value”. The compiler starts processing from main() where main declares “its” value variable with the value 1,but before it can get printed the function fun1() is called. The compiler leaves the main() midway and goes to fun1() where another variable “value” is declared with a value of 2. fun1() contains a call to fun2() so the compiler goes on to call fun2(). The printf() inside fun2() is the first value to be printed which is 3 after which the compiler moves to fun3(). The variable declared inside fun3() has the value 4, this is the second value to be printed. After printing 4 the value is incremented by 1. But since there is no printf() after than 4 is not printed. When fun4() has finished its processing the control returns to the calling function which is fun3(). Now that the processing of fun3() is also finished the program returns to the calling function of fun3() which is fun2(). Inside fun2 the value is decremented by 1, remember that all the declarations are local therefore the decrement will take place on the value declared inside fun1() which is 2. After decrementing the result is printed therefore 1 is the next value to be printed. When fun1 () is also finished the controls move to main() since it called fun1() where again 2 is printed which was the local value of the variable.
Variables which are does not fall under a particular function are called global variables. Such variables are declared outside a function scope. Sometimes all referred to as public variables, global variables are accessible from any function within a program.
Example 1 : Using a global variable
int points=10
void main()
{
void show();
printf(“\nPoints in main %d”,points);
points+=10;
show();
}
void show()
{
printf (“Points In show %d”, points);
}
Output
Points in main 10
Points in show 10
Description
The above example declares a variable above “main ()” named points. Since “points” is declared outside main () or any other function it is not a local variable of any function. The value of points is first printed inside main then the value is incremented by 10 making it 20. When the show () is called it will print the incremented value which is 20. As you can see the value of points is shared by both main () and show () and if it is modified in one function the change can be seen in the other function.
Example 2: Declaring a local and a global variable with the same name.
int sample=100; // Global Variable
void main ()
{
int sample=200; // Local Variable
printf(“\nLocal variable %d”,sample);
printf(“\nGlobal variable %d”,::sample); // printed using double colons ( : : )
}
Output
Local variable 200
Global variable 100
Description
If a program has both local and global variable with the same name, then by default the value of the local variable will be processed. If we want to print the global variable then we must prefix the variable name with the symbol :: , called the scope resolution operator. This operator will help the compiler differentiate between the local and the global variable. The scope resolution operator is not required if both the names are different.
0 comments:
Post a Comment