Skip to main content
Engineering LibreTexts

10.2: Scope - Global vs Local

  • Page ID
    29090
  • Scope of Variables in C++

    (UNDERSTAND THAT IN THESE SIMPLE EXAMPLES WE HAVE NOT PROVIDED PROTOTYPES IN ORDER TO SAVE SPACE)

    In general, the scope is defined as the extent up to which something can be worked with. In programming also the scope of a variable is defined as the extent of the program code within which the variable can we accessed or declared or worked with. There are mainly two types of variable scopes:

    1. Local Variables
    2. Global Variables
    #include <iostream>
    using namespace std;
    
    // This is a global variable
    // It is defined outside of any block of code
    int myGlobal = 5;
    
    int main() 
    {
        // This is a local variable
        int myLocal = 2;
    
        return 0;
    }

    Local Variables

    Variables defined within a function or block are said to be local to those functions.

    • Anything between ‘{‘ and ‘}’ is said to inside a block.
    • Local variables do not exist outside the block in which they are declared, i.e. they can not be accessed or used outside that block.
    • Declaring local variables: Local variables are declared inside a block.
    // CPP program to illustrate 
    // usage of local variables 
    #include<iostream>
    using namespace std;
      
    void thisFunc()
    {   
        // this variable is local to the
        // function thisFunc() and cannot be 
        // accessed outside this function
        int age=18;    
        return;
    }
      
    int main()
    {
        cout << "Age is: " << age << endl;
          
        return 0;
    } 

    Output:

    Error: age was not declared in this scope
    

    The above program displays an error saying “age was not declared in this scope”. The variable age was declared within the function func() so it is local to that function and not visible to portion of program outside this function.


    Problem Solved: To correct the above error we have to display the value of variable age from the function someFunc() only. This is shown in the below program:

    // CPP program to illustrate 
    // usage of local variables 
    #include<iostream> 
    using namespace std; 
    
    void someFunc() 
    { 
        // this variable is local to the 
        // function someFunc() and cannot be 
        // accessed outside this function 
        int age=18; 
        cout<<age; 
    } 
    
    int main() 
    { 
        // We begin the output
        cout<<"Age is: "; 
        // Then call the function to complete it
        someFunc(); 
        
        return 0; 
    } 
    

    In this case, things work just fine, since we are not attempting to access an unknown variable.

    Age is: 18
    

    Global Variables

    As the name suggests, Global Variables can be accessed from any part of the program.

    • They are available through out the life time of a program.
    • They are declared at the top of the program outside all of the functions or blocks.
    • Declaring global variables: Global variables are usually declared outside of all of the functions and blocks, at the top of the program. They can be accessed from any portion of the program.
    // CPP program to illustrate 
    // usage of global variables 
    #include<iostream> 
    using namespace std; 
    
    // global variable 
    int global = 5; 
    
    // global variable accessed from 
    // within a function 
    void display() 
    { 
        cout << global << endl; 
    } 
    
    // main function 
    int main() 
    { 
        // Call display() function - value of global variable is 5
        display(); 
        
        // changing value of global 
        // variable from main function 
        global = 10; 
            // Call display() function - value of global variable is 10
        display(); 
    } 
    

    Global variables are accessible to any function throughout the file.

    5
    10
    

    In the program, the variable “global” is declared at the top of the program outside all of the functions so it is a global variable and can be accessed or updated from anywhere in the program.

    Duplicate names

    What if there is a variable inside a function with the same name as that of a global variable and if the function tries to access the variable with that name, then which variable will be given precedence? Local variable or Global variable? Look at the below program to understand the question:

    // CPP program to illustrate 
    // scope of local variables 
    // and global variables together 
    #include<iostream> 
    using namespace std; 
    
    // global variable 
    int twoNames = 5; 
    
    // main function 
    int main() 
    { 
        // local variable with same 
        // name as that of global variable 
        
        int twoNames = 2; 
        cout << twoNames << endl; 
    } 
    

    Look at the above program. The variable “twoNames” declared at the top is global and stores the value 5 where as that declared within main function is local and stores a value 2. So, the question is when the value stored in the variable named “global” is printed from the main function then what will be the output? 2 or 5?

    Here in the above program also, the local variable named “global” is given precedence. So the output is 2.

    • Usually when two variable with same name are defined then the compiler produces a compile time error. But if the variables are defined in different scopes then the compiler allows it.
    • Whenever there is a local variable defined with same name as that of a global variable then the compiler will give precedence to the local variable

    Scope Resolution

    What if we want to do the opposite of above task. What if we want to access global variable when there is a local variable with same name?

    To solve this problem we will need to use the scope resolution operator. Below program explains how to do this with the help of scope resolution operator.

    // C++ program to show that we can access a global 
    // variable using scope resolution operator :: when 
    // there is a local variable with same name 
    #include<iostream> 
    using namespace std; 
    
    // Global
    int newVal = 0; 
        
    int main() 
    { 
       // Local x     
       int newVal = 10; 
       cout << "Value of global newVal is " << ::newVal; 
       cout << "\nValue of local newVal is " << newVal; 
    
       return 0; 
    } 
    

    Output:

    Value of global newVal is 0
    Value of local newVal is 10

    Definitions

    Global Scope
    Data storage defined outside of a function.
    Local Scope
    Data storage defined inside of a function.
    Data Area
    A part of an object code file used for storage of data.
    Stack
    A part of the computer's memory used for storage of data.
    Scope
    The area of a source code file where an identifier name is recognized.

    Adapted from:
    "Scope of Variables in C++" by Harsh AgarwalGeeks for Geeks is licensed under CC BY-SA 4.0