variables scope:local variables and global variables


Variables scope,local variables and global variables scope

Variables scope in C or C++ mean the region under which a particular variable is alive or it can be utilized.The variable scope is mainly define by where it is declared in a program.It can be inside a function or it can be outside the function.Sometime it can be inside a class in a header file.Depending on where it is declared variables scope is divided into two types:

i)Local variables scope and

ii)Global variables scope.

In this post we will discuss the various region which classify the variables as local scope variables and global scope variable.

Link:What is variables?

Local variables

A variable is said to have a local scope or it is a local variables if it is declared locally.A region is considered as local region if it is found under a braces or the region is own by a function.

i)Region define by a braces

Suppose in a main() function if we use the braces -the opening and closing braces- then the region under the braces is a local region.Consider the variable ‘loc’ declared inside the braces.

int main( )
{
int i=90;

 {//starting of local region

 int loc=89 ;

 cout<< i << endl
 << i ;

 }///ending of local region

cout<< i << endl ///work fine
 << loc ; ///error

return 0;
}

The loc variable exist only inside the braces.Outside the braces region accessing the value is not allowed,so trying to access the value outside it will give an error.The variable ‘i’ has a scope of the whole function main(),the region of main() also engulf the local region surrounded by the braces so it is accessible inside the braces.

A local region can be embedded inside another local region.So there can exists many local regions inside a local region.Consider the code below.

int main( )
{
int i=89 ;

 { //local region 1
 int loc1=88 ;

  {//local region 2
  int loc2=78 ;

  cout<< i << loc1 << loc2 ;

  }//local region 2 ends

 cout<< loc1 << i ;
 cout<< loc2 ;//error

 } ///local region 1 ends

return 0 ;
}

In the code above I have named the local region as region 1 and region 2.Naming a region is a good practice as it create a fine distinction between various local regions and the variables that belong to them.The local region 1 surrounds the local region 2 so the variable of region 2 is not accessible outside the region.

ii)Region own by a function

Whenever a function is defined it owns a region.The region it owns will contain the code that will be executed whenever it is called.If any variables is declared inside the function it is a local variable.The reason is simple, any variable declared inside the function cannot be accessed outside the function.Consider the function given below.

Link:What is function?

void func( )
{
int var=90;

cout<< var_main ;//error
}

int main( )
{
int var_main=89;

cout<< var ;///error
return 0;
}

The variable of func() ,var is inaccessible inside the main() function as it has a local scope in the region own by the function func().Note main() is also a function so the variable var_main cannot be accessed inside func().So var_main also has a local scope.

Inside a function we can again have a local scope surrounded by braces.One instance of this case is also discussed above; local region inside main( ).Here is another example.

void func( )
{
int var=90 ;

{//Local scope Locfunc begins

int var1=930 ;

}///local scope locfunc ends
cout<< var1 ;//error
}

The reason why accessing var1 as shown in the code above gives an error message has the same reason as the other variables discussed:var1 is alive only inside the region locfunc.

If we look logically the concept of local scope of a variable is a relative term.A scope is a local scope if it occupy a smaller region inside another scope.The number of local scope inside another local scope can be infinite and always lies under the influence of another larger -local or global(discussed next)- scope.So here we always compare a smaller scope with a larger scope to come to the conclusion that it is a local scope.


 


A legit program,earn money($$$) from your website-Join Now!

Global variable

A global variable has an influence over the entire region of a particular file;can be .cpp or .h file.By this we mean a variable having a global scope can be accessed anywhere in the file.Whether it is inside a function or may be inside another local scope,the global variable can be used.To make a variable a global variable we declare the variable outside all the scope limiting element.The scope limiting element here refer to all the functions including main() and outside of every braces.A simple program is given below.

/***Your main.cpp file ****/

#include<iostream>

using namespace std;

int global=657 ; ///global variable

void func( )
{
cout<< global ; //work fine
}

int main( )
{
int i=90 ;

cout<< global ;///work fine

 {
 cout<< global ;///work fine
 cout<< i ;///work fine
 }

return 0;
}

So you can see the ‘global’ int type variable is accessible anywhere since it is a global variable.





Global and local variable with the same name

Sometimes it happen that the name you used for global variable and local variable are the same;C++ allows giving the same name to a global and local variable.In such case if you try to access the variable and the local scope variable with the same name has an influence in that region,then the local scope variable will be used.However,if the local variable cease to function then the global variable will be accessed instead.

int val=890 ;

void func( )
{
int val=555;

cout<< val; ///gives 555,local variable is used
}

int main( )
{
 {///local region

 int val=12 ;

 cout<< val ;///gives 12 ,local variable is used
 }//local region ends

cout<< val;///gives 890 ,global variable is used

return 0;
}

So whenever a global and local variable exists in the same scope,the local scope is given a higher priority over the global variable.But there is a way to access the global variable in the region if a local variable with the same name exists.Using the scope resolution operator(::) we can signal the compiler that we want to use the global variable not the local variable.A simple program is shown below.

int value=890;

void func( )
{
int val=555;

cout<< val; ///gives 555
cout<< ::val; ///gives 890,global variable
}

int main( )
{
 {///local region
 int val=12 ;

 cout<< val ;///gives 12 ,local variable is used
 cout<< ::val ;///gives 890 ,global variable

 }//local region ends

cout<< val << endl ///gives 890
<< ::val ; //still gives 890

return 0;
}

Using the scope resolution operator is the best way to distinguished between a global and local variable.It is easy to use and remember.


 



data member of class-is it local or global variable

If you are wondering what scope the data members of a class belong to? they belong to a local variable scope.The data member of a particular class can only be accessed by the object of that class so naturally it has a local scope.

class Scope
{
public:
int i;
Scope(int ii=0):i(ii) { }
} ;

class A{ } ;

int main( )
{
Scope sc(758) ;
cout<< sc.i << endl ;

A a;
cout<< a.i ;///error

return 0 ;
}

Although the data members are considered as having a local scope the objects can be either a local or global object.A simple program is shown below.

class Scope
{
public:
int i ;

Scope(int ii=0):i(ii) { }
} ;

Scope sc(34) ; ///global object

int main( )
{
Scope sc(758) ; ///local object

cout<< sc.i << endl ;///gives 758 ,local object value is accessed
cout<< ::sc.i << endl ;///gives 34 ,global object value is accessed

return 0 ;
}

The scope resolution operator is still functional in the case of class objects to access the global object when the local object has the same name as the global object.


Conclusion

The variables scope:local variable or global variables follows the rule discuss above in a particular file.If you have another file you cannot access the local variables-this goes without saying- and you cannot also access the global variable of another file.If you want to make a global variable of another file available in that file we will employ a different method.The discussion of this method is done in another post;will update the post soon.In the meanwhile happy programming!


Related Link

->Some terms:Variables , Identifier , Keyword , Operators , Operands,literals.