Exforsys.com
 

Sponsored Links

 

C++ Tutorials

 
Home Tutorials C++
 

Scope of Variables in Function

 
Category: C++
Comments (1)

Scope of Variables in Function

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



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:


    #include <iostream.h>
    int exforsys(int,int);
    void main( )
    {
        int b;
        int s=5,u=6;
        b=exforsys(s,u);
        cout<<”\n The Output is:”<<b;
    }

    int exforsys(int x,int y)
    {
        int z;
        z=x+y;
        return(z);
    }


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


    #include <iostream.h>
    int x,y,z;      
//Global Variables
    float a,b,c;    
//Global Variables
    void main( )
    {
        int s,u;    
//Local Variables
        float w,q;
       //Local Variables
        ……..
        …………….
    }


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.



    #include <iostream.h>
    int g;
    void main( )
    {
        int a;
        {          
            int b; 

            b=25;  
            a=45;  
            g=65;  

        }
        a=50;      
        exforsys( );
    } 
            

    void exforsys( )
    {
        g = 30;
//Scope of g is throughout the program and so is used between function calls
    }


Scope of b is till the first braces shaded as Yellow


Scope of a is till the end of main brace shaded as red


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


ruban_stalin said:

  Nice explanation. can you explain about scope resolution operator?
November 24, 2006, 12:24 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape