Exforsys

Home arrow Technical Training arrow C++ Tutorials

C++ Variables Scope in Functions

Author: Sripriya R     Published on: 17th Oct 2006    |   Last Updated on: 25th Jul 2011

In this C++ Tutorial you will learn about Scope of Variables in Function, Local Variables - Scope of Local Variables, Global Variables - Scope of Global Variables.

Ads

The scope of the variables can be broadly be classified as

  • Local Variables
  • Global Variables

Local Variables:

The variables defined local to the block of the function would be accessible only within the block of the function and not outside the function. Such variables are called local variables. That is in other words the scope of the local variables is limited to the function in which these variables are declared.

Let us see this with a small example:

Sample Code
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int exforsys(int,int);
  5. void main( )
  6. {
  7.         int b;
  8.         int s=5,u=6;
  9.         b=exforsys(s,u);
  10.         cout << "n The Output is:" << b;
  11. }
  12.  
  13. int exforsys(int x,int y)
  14. {
  15.         int z;
  16.         z=x+y;
  17.         return(z);
  18. }
Copyright exforsys.com


The output of the above example is:

In the above program the variables x, y, z are accessible only inside the function exforsys( ) and their scope is limited only to the function exforsys( ) and not outside the function. Thus the variables x, y, z are local to the function exforsys. Similarly one would not be able to access variable b inside the function exforsys as such. This is because variable b is local to function main.

Global Variables:

Global variables are one which are visible in any part of the program code and can be used within all functions and outside all functions used in the program. The method of declaring global variables is to declare the variable outside the function or block.

For instance

Sample Code
  1. #include <iostream.h>
  2.     int x,y,z;       //Global Variables
  3.     float a,b,c;     //Global Variables
  4.     void main( )
  5.     {
  6.         int s,u;     //Local Variables
  7.         float w,q;       //Local Variables
  8.         ...
  9.         ...
  10.     }
Copyright exforsys.com


In the above the integer variables x, y and z and the float variables a, b and c which are declared outside the block are global variables and the integer variables s and u and the float variables w and q which are declared inside the function block are called local variables.

Thus the scope of global variables is between the point of declaration and the end of compilation unit whereas scope of local variables is between the point of declaration and the end of innermost enclosing compound statement.

Let us see an example which has number of local and global variable declarations with number of inner blocks to understand the concept of local and global variables scope in detail.

Sample Code
  1. ...
  2. int g;
  3. void main( )
  4. {      
  5.         ...
  6.         int a;
  7.         {          
  8.                 int b;  
  9.                 b=25;  
  10.                 a=45;  
  11.                 g=65;   // end of scope for variable b
  12.         }
  13.         a=50;      
  14.         exforsys( );
  15.         ...
  16. }// end of scope for variable a            
  17.  
  18. void exforsys( )
  19. {
  20.         g = 30; //Scope of g is throughout the program and so is used between function calls
  21. }
Copyright exforsys.com


Ads

In the context of scope of variables in functions comes the important concept named as storage class which is discussed in detail in next section.

Read Next: C++ Inheritance


 
 

Comments