In programming, we often use variables to store some value. Basically, variables are the names of a memory location. In static programming, we first define a variable and then use it. However, in dynamic programming, we do not have to define a variable before using it. In this article, we have compared two types of variables - local variable and global variable. But before proceeding with the comparison, let us understand the scope of a variable.
Scope of a Variable
The scope of a variable defines the lifespan of that variable. In simple words, the scope of a variable defines how long the variable will exist during the program execution. When we create a program, we often make use of user-defined functions. Inside those functions, we use variables that are accessible to only the particular function that defines them.
The lifespan of a simple user-defined function starts at the time when the main function calls it, and its life ends at the moment when it returns the control or value back to the main function. As the function lifetime end when it returns a value, so all the variables associated with it end too.
For example:
int fun() { int j=1; return j; } void main() { ////////// }
Here in the above example the scope or lifecycle of variable j depends upon when the main function that calls the fun () function. The life cycle or scope of the variable j is limited to the function fun(). The scope of the variable can also define the type of that variable:
- A variable defined inside a function is known as a local variable
- A variable defined outside the function is known as a global variable.
- If the variable is defined inside the function parenthesis, it will be considered as a formal variable.
Global vs Local Variables
Local Variable
All those variables that are defined inside a function block are considered as local variables. In programming languages like C, Python, C++, and Java, the theory for a local variable is the same. However, in some cases, a local variable in Python can show maverick behavior because Python is a dynamically-typed language.
One of the important characteristics of a local variable is that it is available within the function block in which it is declared. No other function would be able to access that variable. Also, when it comes to local variables, we can assign the same variable name to different data types across several functions and there would be no conflict.
As we have mentioned that a local variable would be only available within its function block, so its scope and lifecycle are also associated with that function. A local variable will come into existence when the function calls it and get destroyed at the moment when the function returns a value or give control back to the main function. All the local variables get stored in the stack memory along with their functions.
Example:
void func_1() { int local_variable; local_variable=100 ; } void func_2() { char local_variable; local_variable = 'a'; }
Behind the code: In the above example, both the variables inside the functions are local variables. Additionally, they have the same name but different data types. They are local variables because they bind to different functions.
Global Variable
Variables that are not bound to any particular function or defined outside the function blocks are termed global variables. Unlike a local variable, any function can access a global variable at any part of the code. However, there is one condition that global variables need to fulfill; they should be outside all the functions in a program .
As any function can access the global variable, the life cycle or scope of a global variable remains in existence until the end of the whole program. A global variable does not store in the stack memory, and it has its own memory region that is decided by the compiler .
Example :
int g=10; void func_1() { int v; v=100 ; cout<<g; } void func_2() { char l; l = 'a'; cout<<g; }
Behind the code: In the above example, variable g is a global variable and v & l are local variables.
Global vs Local Variables: Head to Head Comparison
Local Variable |
Global Variable |
Declaration |
|
Local variable declares inside the function block. | Global variable declares outside the function block. |
Scope |
|
The scope of a local variable is limited to the function which defines it. | The scope of the global variable exists throughout the program. |
Accessibility |
|
Only the function that defined a local variable can access it. | Any function can access a global variable. |
Life Cycle |
|
The life of a local variable begins when its function is called, and ends when the function gets terminated. | The life cycle of a global variable remains in existence until the complete execution of the program. |
Memory efficient |
|
Local variables are more memory efficient because they get destroyed when their work is done. | The global variable remains until the completion of the whole program and thus occupies memory even if they are not required. |
Value Modification |
|
There is no such case of value modification when using two different functions with each having a local variable with the same name. | When two functions use the same global variable, there value modification will be reflected on the global variable. |
Memory Storage Location |
|
The stack memory stores a local variable along with its functions. | The global memory region assigned by the compiler itself stores the global variables. |
Advantages: |
|
|
|
Disadvantages |
|
|
|
To Sum it Up
As a programmer, it is quite important for you to understand the difference between a local variable and a global variable. Also, you need to know about the scopes of both variables because it will help you decide which one to use at a particular instance within your code.
People are also reading:
Leave a Comment on this Post