As we discussed earlier, a variable is a name, given to a memory location, and it must be declared before it is used. In C, all the variables are declared at the starting of the program. In C++, variables can be declared, at any point of time, before they are used in the instructions.
Variables are classified into ‘local’ and ‘global’ variable, which is the main topic of our discussion. Here the main difference between local and global variable is that a local variable is declared inside a function block. In contrast, the global variable is declared outside the functions in the program.
Let’s study some more differences between a local and a global variable along with a comparison chart.
Content: Local Variable Vs Global variable
|BASIS FOR COMPARISON||LOCAL VARIABLE||GLOBAL VARIABLE|
|Declaration||Variables are declared inside a function. ||Variables are declared outside any function.|
|Scope ||Within a function, inside which they are declared.||Throughout the program.|
|Value||Uninitialized local variable result in storage of the garbage value.||Uninitialized global variable stores zero by default.|
|Access||Accessed only by the statements, inside a function in which they are declared.||Accessed by any statement in the entire program.|
|Data sharing||Not provided||Facilitated|
|Life||Created when the function block is entered and destroyed upon exit.||Remain in existence for the entire time your program is executing.|
|Storage ||Local variables are stored on the stack unless specified. ||Stored on a fixed location decided by a compiler.|
|Parameter passing||Necessarily required||Not required for global variables.|
|Changes in a variable value||Any modification implied in a local variable does not affect the other functions of the program.||The changes applied in the global variable of a function reflect the changes in the whole program.|
Definition of Local Variable
A local variable is always declared inside a function block. In C, a local variable is declared at the start of a code block. In C++, they can be declared anywhere in the code block prior to their use.
Local variables can be accessed only by the statements written inside a function in which the local variables are declared. They are secure in a sense that, they cannot be accessed by any other function of the same program.
Local variable exist till the block of the function is in execution, and thereby destroyed after the execution exits the block. Local variables lose their content as soon as the execution left the block in which they are declared.
The reason behind it is that the local variables are stored on the stack unless their special storage is specified. The stack is dynamic in nature, and the change in memory location leads to the reason why the local variable doesn’t hold their value as soon as the block of a function exists.
However, there is a way to retain the value of a local variable by using the ‘static’ modifier.
Definition of Global Variable
A global variable is declared outside all the functions present in a program. Unlike local variables, the global variable can be accessed by any function present in a program. Global variables are not much reliable as their value can be changed by any function present in the program.
They remain in existence until the whole program gets executed completely. Global variables retain their values until the program is in execution. The reason is that they are stored on a fixed region of memory, decided by the compiler.
A Global variable is helpful in situations where multiple functions are accessing the same data. Using a large number of global variables might be problematic, as there may be unwanted changes to the value of a global variable.
Key Difference Between Local and Global Variable
- Local Variables are called ‘local’ because they are only known to the statements written in a function inside which they are declared and not known to any other function present outside that function block. In the case of a global variable, they are known to each and every function present in a program; hence, they are called ‘global’.
- Global variables retain their value until the program is in the execution phase, as they are stored at a fixed location decided by the compiler. Local variables are stored on the stack; hence, they do not retain their value as ‘stack’ is dynamic in nature, but the compiler can be directed to retain their value, by using the ‘static’ modifier.
- If a global and a local variable are declared with the same name then, all the statements of a code block in which local variable is declared will refer only to a local variable and will cause no effect to a global variable.
- A local variable is destroyed when the control of the program exit out of the block in which local variable is declared. However, a global variable is destroyed when the entire program is terminated.
- The main benefit of a local variable is that there is no accidental alteration of the data. The variable is declared inside a block, and these block of code uses the variable and avoid undesirable side effects.
- Local variable consumes memory for a limited amount of the period, only when the block containing the variable is executed.
- Global variables are very useful when you are dealing with several functions in the program manipulating the same data.
- Changes that needed to be applied in the entire program would be easier through implementing a global variable.
- We can access from anywhere or through any random function of the program.
- The scope of the local variable is restricted.
- Forbids data sharing.
- They are not able to retain the data between the calls because local variables are generated and removed with each entry and exit from the block. However, the static modifier can be used to retain the values.
- Use of a large number of global variables could result in the generation of program errors.
- The main problem that it causes is the accidental occurrence of the changes due to the disseminated global variables throughout the program.
- It could also rise the need to conduct code refactoring, which is a very extensive process where the entire program code is restructured.
The local and global variables both are necessary and equally required while writing the program. However, declaring a large number of global variables could be problematic in a massive program, as it may cause unwanted changes to a global variable; and it would become hard to identify that which part of a program made that change. Hence, one should avoid declaring unnecessary global variables.