# 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.