Global vs Local Variables

By | August 24, 2019

In programming we often use variables to store some value, basically, variables are the names given to a memory location. In static Programming, we first define a variable then use it, but in dynamic Programming, we do not have to define a variable before using.

Here in this article, we have compared two types of variables Local variable and Global Variable. Before comparing both variables let have a look at what is a Scope of a Variable.

Scope of a Variable

Scope of a variable defines the lifetime of that variable during the execution of a program. In simple words scope of a variable defines how long that variable will exists during the program execution.

When we create a program, we often use user-defined functions and inside those functions we use variables which are accessible to only that particular function which define them.

The lifetime 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 variable associated with it ends 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 calling the func() function. the life circle or scope of the variable j is limited to the function func().

The scope of the variable can also define the type of that variable:

  • If a variable is defined inside a function it will be considered as a local variable
  • If a variable is defined outside the function it will be considered as Global variable
  • If the variable is defined inside the function parenthesis it will be considered as a formal variable.

Global vs local variables

But in this article, we are only comparing Global and Local variables:

Types of Variables:

  • Local Variable
  • Global Variable

Local Variable

All those variables which are defined inside a function block consider as a local variable. In programming languages like C, Python, C++, and Java the theory for a local variable is same, but in some cases, Python local variable can show maverick behavior because python is a dynamic type language.

One of the important characteristics of a local variable is, that variable would only available for that function block, no other function would be able to access that variable. Local variable solves the problem of the namespace in programming.

With the help of a local variable name, we can assign the same variable name to the different data types in different functions and there would be no conflict.

As we have mentioned that a local variable would be only available for its function block, so its scope and lifecycle is also associated with that function, the local variable will come in existence when the function would call and get destroyed at the moment when the function returns a value or give control back to the main function.

All the local variable gets stored in the stack memory along with their function.

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 variable inside the functions are local variable but posses same name and different data types. They are local variables because they bind to different functions.

Global Variable

Those variables which are not bound to any function or defined outside the function blocks those variables are termed as global variables.

Unlike a Local variable any function can access a Global variable at any part of the code, but there is one condition the global variable must be defined before any other function.

As any function can access the global variable so the life cycle or scope of a global variable remains in existence until the complete program gets executed. A global variable does not store at stack memory it has its own memory region, 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.

Local vs Global Variable: Head to Head Comparison

Local

Global

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 can access the local variable which defined that variable. Any function can access the global variable

Life Cycle

The life of the Local variable begins when the function is called and ends when the function gets terminated. The life cycle of a global variable remains in existence until the program gets completely executed.

Memory efficient

Local variables are more memory efficient because they get destroyed when their work is done The global variable remains until the complete execution and occupies memory even if they are not required.

Value Modification

Value of one local variable defined inside a function has no effect on a local variable defined in other function so there is no such case of value modification when using two different functions with the same local variable name. When two functions use the same global variable the value modification will be reflected on the global variable

Memory Storage Location

Local Variable get stored in stack memory along with their functions Global variable store in Global memory region assigned by the compiler itself.

Advantages:

  •  The value will remain limited to that particular function.
  • Two local variables can have the same name if they are declared in different function this will resolve the shortage of variable name.
  • The variable gets destroyed when the function gets completely executed this save memory.
  • We can access the variable in any function.
  • Define it one time and use anywhere in the program
  • Often use a global variable to save a constant value which does not require any modification

 

Disadvantages

  • Debugging of a local variable could be complex when the program contains thousands of lines
  • It has limited scope
  • It is not that much memory efficient and too many global variables just occupy the memory.
  • Its value can be modified this can cause a problem when you want your global variable to be constant.

People Also Read:

Leave a Reply

Your email address will not be published. Required fields are marked *