Scope Variable in C++ - Learn C++ , C++ Tutorial , C++ programming




 scope-variable-in-c++

Learn c++ - c++ tutorial - scope variable in c++ - c++ examples - c++ programs

Scope:

  • Scope applies to identifiers including variable names, functions names, class names.
  • A variable’s scope determines where a variable is accessible.
  • A variable’s duration determines where it is created and destroyed.
  • The scope of an identifier is the region of a source program within which it represents a certain thing.
  • Scope is the context that gives meaning to a name

Local Variable:

  • A variable defined inside a function (defined inside function body between braces) is called a local variable or automatic variable.
  • learn c++ tutorials - global variable in c++

    learn c++ tutorials - global variables in c++ Example

  • Its scope is only limited to the function where it is defined. In simple terms, local variable exists and can be accessed only inside a function.
  • The life of a local variable ends (It is destroyed) when the function exits.

Example 1: Local variable

#include <iostream>
using namespace std;

void test();

int main() 
{
    // local variable to main()
    int var = 5;

    test();
    
    // illegal: var1 not declared inside main()
    var1 = 9;
}

void test()
{
    // local variable to test()
    int var1;
    var1 = 6;

    // illegal: var not declared inside test()
    cout << var;
}
  • The variable var cannot be used inside test() and var1 cannot be used inside main()function.
  • Keyword auto was also used for defining local variables before as: auto int var;
  • But, after C++11 auto has a different meaning and should not be used for defining local variables.

Variables defined inside nested blocks are destroyed as soon as the inner block ends:

int main() // outer block
{
    int n(5); // n created and initialized here
 
    { // begin nested block
        double d(4.0); // d created and initialized here
    } // d goes out of scope and is destroyed here
 
    // d cannot be used here because it was already destroyed!
 
    return 0;
} // n goes out of scope and is destroyed here

Variables in one function cannot be seen from another function:

  • Variables defined inside a block can only be seen within that block. Because each function has its own block
void someFunction()
{
    int value(4); // value defined here
 
    // value can be seen and used here
 
} // value goes out of scope and is destroyed here
 
int main()
{
    // value can not be seen or used inside this function.
 
    someFunction();
 
    // value still can not be seen or used inside this function.
 
    return 0;
}

Shadowing:

  • Shadowing is when a block scope variable with the same name as a file scope variable or an ``outer'' block scope hides or shadows those variables and will be the one to be referenced.

Shadowing example:

void func()
{
   int abc = 5;
   {
       int abc = 9;    // The scope of this abc is the block around it
       cout << abc;    // 9 is output
   }
}

Related Searches to Scope C++