Scope of a variable
what is the scope of a variable? The scope of a variable can be defined as the accessibility of a variable in the program. It means whenever a variable is defined in a program, it is not accessible in every portion of it. It might be accessed in some portion or in all portion of it. Another name for scope is visibility. So scope of a variable is actually the part of a program where it is visible. For better understanding the concept, lets consider two cases.
int firstvariable=10; //valid
int secondvariable=11; //valid
thirdvariable=9; // not valid
In the above definition of function, the fisrvariable and secondvariable are visible to the “afunction()” but the third variable is unknown to it. Limiting the visibility in programming has many benefits. First of all it helps you to provide security. Secondly, it helps the programmer to avoid mixing the values of different variables by different functions. This is an important feature of structured programming. It is also an important feature of object oriented programming.
Types of variables based on visibility
- Local Variable
- Global Variable
- Static Local Variable
Local Variables scope
Local variables are those that are local to the function definition. It means they are only accessible where they have been declared. They are not accessible to the other functions defined in the same program.
var1=5; // not accessible as it is locally declared in somefunction().
Global Variable scope
Another name for the global variables is the external variables. Global variables are those that are declared externally i.e., outside the functions. They are declared in the start of the program. That’s why they are visible to all functions used within a program.
As it can be seen the variable “age” is declared at the top of the program and it is accessible to both functions. The first function sets the value of age and the second function displays its value.
Lifetime and visibility
The global variables belongs to the static storage class which means they exist throughout the program. They are not like the local variables that are created only at the time of a function call and then destroyed once the function is executed.
Static Local Variables scope
If we want our program to remember the value of local variable then we can declare the variable as static. For this static keyword is used before it. static local variables come into existence once the definition of the function begins where they are declared. Once the function is executed the program remembers its value. So the lifetime of static local variable is same as that of global variables. The following example illustrates the concept of static local variable.
float d1=1; average;
cout<<“enter the value”<<endl;
cout<<“new average value is”<<average<<endl;
float takeavg(float newdata)
static float t1=0;
static int c1=0;
both static variables t1 and c1 retain their values after the function takeavg() returns, so they are available the next time if needed.
Example of scope of variable
int x=10; //this is the global variable as it is outside the main function
int x=6; // here x is local variable as it hides global variable x=10
int y=::x; // y=10 here x is global because of :: operator
int z=x; // z=6 (local variable)
int x=38; // now value of x is hided again (x=6)
int t=::x; // t=10
t=x; // t=38 here x is treated as local variable
int z=x; z=6
From above discussion it is concluded that the scope of variable is chosen by the programmer as per the need. If we want to keep the value accessible throughout the program then it is preferable to declare them as global variables.
you can also read here: