20 Apr 2017

Difference Between Local and Global Variable


keyword and identifierAs 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 further classified into ‘local’ and ‘global’ variable, which is the main topic of our discussion. Here the main difference between a local and a global variable is that, a local variable is declared inside a function block, where as 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

  1. Comparison Chart
  2. Definition
  3. Key Differences
  4. Conclusion

Comparison Chart:

BASIS FOR COMPARISONLOCAL VARIABLEGLOBAL VARIABLE
DeclarationVariables are declared inside a function.Variables are declared outside any function.
ScopeWithin a function, inside which they are declared.Throughout the program.
AccessAccessed only by the statements, inside a function in which they are declared.Accessed by any statement in the entire program.
LifeCreated when the function block is entered and destroyed upon exit.Remain in existence for the entire time your program is executing.
StorageLocal variables are stored on the stack, unless specified.Stored on a fixed location decided by a compiler.

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 variable 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 local variable doesn’t hold their value as soon as the block of a function exists.
Note:
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.
Global variables remain in existence till the whole program get executed completely. Global variables retain their values till 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.

  1. 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 global variable they are known to each and every function present in a program; hence, they are called ‘global’.
  2. Global variables retain their value till 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.
  3. 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.
  4. 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.

Conclusion:

The local and global variables both are equally required while writing the program. But, declaring a large number of global variables could be problematic in a large 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.

No comments:

Post a Comment

commnet here