In any programming language, the scope is a region which is can be defined as the extent up to which something is valid. All the variables used in a program have their extent of use and if used out of that extent or boundary, they do not hold their values. Hence this boundary is referred to as the scope of a variable
These boundaries are referred to curly braces “{}
“ in a program. Based on their scope, there are two types of variables.
- Local variables: inside a function or a block.
- Global variables: outside of all functions.
- Formal parameters: function parameters.
- block: refers to anything between ‘{‘ and ‘}’.
1. Local variables:
Local variables are the variables that are declared within the body of the function or block. These types of variables can only be used within a block where it is created or declared, it has no existence outside the block.
Example: C++ example using local variables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <iostream> using namespace std; //user-defined function int add(int a, int b) { return a + b; } //main function int main() { /*local variables only used inside main function*/ int x = 10, y = 20; int z; z = add(x, y); cout << "value of z: " << z; return 0; } |
Output:
1 | value of z: 30 |
Let us see one more example where the variable with same name can be used in different function holding different values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <iostream> using namespace std; void add() { int x = 2, y = 10, result; result = x + y; cout << "Addition result: " << result; } void multiply() { int x = 3, y = 5, result; result = x * y; cout << "\nSubstraction result: " << result; } //main function int main() { add(); multiply(); return 0; } |
Output:
1 2 | Addition result: 12 Substraction result: 15 |
2. Global Variable
These variables are available to use throughout the program that is they have their scope throughout the program. They are defined outside the function (including main()
), usually, you will see them at the top of any program.
After its declaration in a program, a global variable can be used inside any function present in a program.
Example: C++ example using global variables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <iostream> using namespace std; // Global variable declaration: int g; int main () { // Local variable declaration adn intialization: int a = 20, b = 30; //use of global variable g = a + b; cout << "g holds: " << g; return 0; } |
Output:
1 | g holds: 50 |
Let us see one more example where the local variable takes preference.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <iostream> using namespace std; //global variable int g = 20; void testFunction() { //overrides the g value with 30 g = 30; cout << g << endl; } int main() { testFunction(); //local variable int g = 50; cout << g << endl; return 0; } |
Output:
1 2 | 30 50 |
As you can see in the above program, g
is declared global with a value of 20, but again it is initialized inside testFunction()
with the value of 30. And present value(30) overrides the previous value (20) and prints 30.
In the main function again g
is defined and initialized (making it local variable) with 50 that overrides the 20.