logo codesdope

Scope of variables


Scope is a region of a program. Variable Scope is a region in a program where a variable is declared and used.

Variables are thus of two types depending on the region where these are declared and used.

Local Variables


Variables that are declared inside a function or a block are called local variables and are said to have local scope. These local variables can only be used within the function or block in which these are declared.

For functions, local variable can either be a variable which is declared in the body of that function or can be defined as function parameters in the function definition

Now let's see an example where a local variable is declared in the function definition.

#include <iostream>
using namespace std;

int multiply(int a, int b){
        return a * b;        
}

int main() {
        int x = 3, y = 5;
        int z;
        z = multiply( x, y );
        cout << z << endl;
        return 0;
}
Output
15

In this example, variables a and b are declared in the definition of the function multiply and are used within the function. These have no meaning outside the function. 'a' and 'b' are the copies of the variables 'x' and 'y' respectively and store their respective values. Thus, any change in the values of 'a' and 'b' does not affect the values of 'x' and 'y'.
Here, 'a' and 'b' are the local variables for the function 'multiply' and 'x' and 'y' are the local variables for the function main.

Let's take one more example to see that local variables are assets of the function in which these are declared.

#include <iostream>
 
using namespace std;
 
void func1(){
	int x = 4;
	cout << x << endl;
}
 
void func2(){
	int x = 5;
	cout << x << endl;
}
 
int main(){
	func1();
	func2();
	return 0;
}
Output
4
5

In the function func1, we declared a variable x and initialized it with a value 4. This variable is in the body of the function func1 and thus gets destroyed when the body of the function func1 ends. So, when we called func1, 4 gets printed.
We declared another variable x in the function func2 and gave it a value 5. This variable also gets destroyed as the body of func2 ends and has no relation with the variable x of func1.
So, the two variables are independent of each other and are limited to only the function in which these are declared.

Same as function, variables declared inside a block are also local variables and are accessible only within the block.

A block is a group of statements enclosed within the curly braces { }. For example, the body of a function is a block. The body of loops or conditional statements like if..else is also a block.

Global Variables


Variables that are defined outside of all the functions and are accessible throughout the program are global variables and are said to have global scope. Once declared, these can be accessed by any function in the program.

Let's see an example of a global variable.

#include <iostream>

using namespace std;

int g;

int main(){
	int a = 4;
	g = a * 2;
	cout << g << endl;
	return 0;
}
Output
8

Here, g is a global variable since it is declared outside of the main function. Thus unlike the local variable 'a' which can only be used in the function main, 'g' can be used throughout the program and can be used in all the functions in the program. In this example, we declared 'g' outside of all the functions and gave it a value in the function.

#include <iostream>

using namespace std;

int g = 10;

void func1(){
	g = 20;
	cout << g << endl;
}

int main(){
	func1();
	g = 30;
	cout << g << endl;
	return 0;
}
Output
20
30

Same as we can change the value of any local variable in a function any number of times and the value last assigned overrides the previous value, we can also override the value of the global variable in a function.
We assigned a value 10 to g at the time of its declaration. Firstly, we called the function func1 in the main function in which we assigned 20 to 'g'. Thus, the value of 'g' became 20 in the function func1 and thus 20 got printed.
After that, we assigned 30 to 'g' in the main function thus making the value of 'g' 30 in the main function and printing 30.

Programming is a skill best acquired by practice and example rather than from books.
-Alan Turing

Share what you know
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.