10 Difference Between Local Variable And Global Variable

SHARE

What is Local Variable?

A local variable is a type of variable that can be used where the scope and extent of the variable is within the method or statement block in which it is declared. It is used as an iteration variable in the foreach statement, exception variable in the specific-catch clause and resource variable in the using statement. It can also be used as a constant whose value cannot be modified within the method or statement block in which it is declared.

Local variables are fundamental  to procedural programming and more generally modular programming: variables of local scope are used to avoid issues with side-effects that can occur with global variables.

In most languages, local variables are automatic variables stored on the call stack directly. This means that when a recursive function call itself, local variables in each instance of the function are given distinct addresses. Hence variables of this scope can be declared, written to, and read, without any risk of side-effects to functions outside of the block in which they are declared.

What You Need To Know About Local Variable

  • A local variable is created when the function is executed and once the execution is finished, the variable is destroyed.
  • Local variables can be accessed with the help of statements inside a function in which they are declared.
  • Local variables are declared at the beginning of any blocks or functions within the program.
  • If the local variable is not initialized, it takes the garbage value by default.
  • Local variables are stored in a stack memory unless specified.
  • Any change in the local variable does not affect other functions of the program.
  • Data sharing is not possible as data of the local variable can be accessed by only one function.  
  • When the value of the local variable is modified in one function, the changes are not visible in another function.
  • Parameters passing is required for local variables to access the value in other function.
  • The name given to local variables in any different functions can be the same or different as these functions are made available to only those functions which they are declared within that function.  

What is Global Variable?

 Global variables, as the name implies, are variables that are accessible globally, or everywhere throughout the program. Once declared, they remain in memory throughout the runtime of the program. This means that they can be changed by any function at any point and may affect the program as a whole.

A group of global variables is called a global state or global environment because when combined, they define various aspects of a program or the environment when the program runs. A global variable is usually declared on top of all functions and is kept to a minimum, as all functions can manipulate them during the program’s run time, which is considered dangerous by most programmers because they may accidentally be changed, resulting in bugs.

What You Need To Know About Global Variable

  • A global variable exists in the program for the entire time the program is executed.
  • It can be accessed throughout the program by all the functions present in the program.
  • Global variables are declared usually at the beginning of any program before defining any function or blocks.
  • If the global variable is not initialized, it takes zero by default.
  • Global variables are stored in the data segment of memory, decided by the complier.
  • Any change in global variable affects the whole program, wherever it is being used.
  • Data sharing is possible as multiple functions can access the same global variable.
  • When the value of the global variable is modified in one function changes are visible in the rest of the program.
  • Parameters passing is not necessary for a global variable as it is visible throughout the program.
  • The name given to the global variable cannot be changed while accessing it at any point of the program as it’s declared and defined only once  at the starting of the program.

Difference Between Local Variable And Global Variable In Tabular

BASIS OF COMPARISON LOCAL VARIABLE GLOBAL VARIABLE
Access Local variables can be accessed with the help of statements inside a function in which they are declared.   Global variable can be accessed throughout the program by all the functions present in the program.  
Creation A local variable is created when the function is executed and once the execution is finished, the variable is destroyed.   A global variable exists in the program for the entire time the program is executed.  
Declaration Local variables are declared at the beginning of any blocks or functions within the program.   Global variables are declared usually at the beginning of any program before defining any function or blocks.  
Initialization If the local variable is not initialized, it takes the garbage value by default.   If the global variable is not initialized, it takes zero by default.  
Stored Local variables are stored in a stack memory unless specified.   Global variables are stored in the data segment of memory, decided by the complier.  
Alteration Any change in the local variable does not affect other functions of the program.   Any change in global variable affects the whole program, wherever it is being used.  
Data Sharing Data sharing is not possible as data of the local variable can be accessed by only one function.    Data sharing is possible as multiple functions can access the same global variable.  
Modification of Value When the value of the local variable is modified in one function, the changes are not visible in another function.   When the value of the global variable is modified in one function changes are visible in the rest of the program.  
Parameters Passing Parameters passing is not necessary for a global variable as it is visible throughout the program.   Parameters passing is not necessary for a global variable as it is visible throughout the program.  
Naming The name given to local variables in any different functions can be the same or different as these functions are made available to only those functions which they are declared within that function.    The name given to the global variable cannot be changed while accessing it at any point of the program as it’s declared and defined only once  at the starting of the program.  

Advantages And Disadvantages of Local Variable

Advantages

  • Local variables use memory only for the limited time when the function is executed; after that same memory location can be reused.
  • Use of local variables offers a guarantee that the values of variables will remain intact while the task is running.
  • Local variables can be given the same name in different functions because they are only recognized by the function they are declared in.

Disadvantages

  • The process of debugging a local variable is quite complex.
  • Data sharing is not possible between modules and thus common data is required to pass repeatedly.
  • Local variables have a very limited scope.  

Advantages And Disadvantages of Global Variables

Advantages

  • Only a single declaration is required.
  • Global variables can be accessed by all the modules present in the program
  • Very appropriate if all functions are accessing the same data.
  • It can make a program easier to read in reference to constants.

Disadvantages

  • Any statement in a program can change the value.
  • If global variables are discontinued due to code refactoring, you will need to change all modules where they are called.
  • It makes debugging harder.
  • If modules use global variables it is dependent upon the module and if other modules are presented you have to redesign each one all over each time.