C++ - Scope of Variables
A variable can only be accessed in the area in which it is defined, which is called scope of the variable. In C++, there are mainly three types of variable scopes:
- Local scope
- Global scope
- Function Prototype Scope
Local Scope
If a variable is created inside a function or a block { .. }, it is called local variable and has local scope. A local variable can only be used inside that function or block.
Example
In the example below, a local variable called str is created and used inside the main function.
#include <iostream> #include <string> using namespace std; int main () { //local variable created //inside main function string str = "Hello World!"; cout<<str; return 0; }
The output of the above code will be:
Hello World!
Local variable outside function
A local variable can not be used outside the block/function in which it is defined and can be used only inside that block/function.
Example
In the example below, a local variable called str is created inside the main function and user-defined function called MyFunction. When str is printed, it gives different results.
#include <iostream> #include <string> using namespace std; void MyFunction() { //local variable created inside //MyFunction function. It is only //accessible inside this function string str = "Hello C++"; cout<<str<<"\n"; } int main () { //local variable created //inside main function string str = "Hello World!"; cout<<str<<"\n"; MyFunction(); return 0; }
The output of the above code will be:
Hello World! Hello C++
Global Scope
If a variable is created outside all functions, usually on top of the program, it is called global variable and has global scope. A global variable can be used anywhere, inside the function and outside the function.
Example
In the example below, a global variable called str is created and used inside the main and MyFunction functions.
#include <iostream> #include <string> using namespace std; //global variable created outside //all functions string str = "Hello World!"; void MyFunction() { //accessing global variable cout<<str<<"\n"; } int main () { MyFunction(); //accessing global variable //and modifying it str = "Hello C++"; MyFunction(); return 0; }
The output of the above code will be:
Hello World! Hello C++
Local Variable with same name as Global Variable
If a variable with same name as global variable is created inside the function, it will be a local variable for that function. Any operation performed on this local variable will not change the global variable. Please note that when a function contains local and global variable with same name, the local variable will take precedence over global variable.
Example
In the example below, a global variable called str is created. A local variable with the same name is also created inside the main function. Any operation performed on this local variable do not change the global variable.
#include <iostream> #include <string> using namespace std; //global variable created outside //all functions string str = "Hello World!"; void MyFunction() { //accessing global variable cout<<str<<"\n"; } int main () { MyFunction(); //creating local variable //with same name string str = "Hello C++"; MyFunction(); cout<<str<<"\n"; return 0; }
The output of the above code will be:
Hello World! Hello World! Hello C++
Function Prototype Scope
The scope of the these variables begins right after the declaration in the function prototype and runs to the end of the declarations list. These scopes don’t include the function definition, but just the function prototype.
Example
The example below shows all types of scope of variables.
#include <iostream> using namespace std; //global scope int MyInt = 100; //function prototype scope void MyFunction(int MyInt) { MyInt = MyInt + 10; cout<<MyInt<<"\n"; } int main () { //local scope int x = 500; cout<<MyInt<<"\n"; MyFunction(x); cout<<x<<"\n"; return 0; }
The output of the above code will be:
100 510 500