Local vs Global variables: Difference, scope and Example

You must be dealing with variables if you are a programmer. Variables can be classified into types – Global variables and Local variables. What are local and global variables and what is the difference between local and global variables, we will see this in detail here.

What is the Difference between Local and Global variables?

The local variable is a variable inside function, all variables which are declared inside a function are local variables while the Global variable is a variable outside the function.

A local variable can be accessed only inside a function while a global variable can be accessed from anywhere in the program.

Let’s understand the absolute difference between local and global variables in detail with scope and Examples. Before understanding types, you should know what are variables.

What is a variable?

In programming, variables are the place that can be used to store values. Variables can be declared and used to keep some values.

An example of a variable is:

Var a =10;

Now let’s know the definition of Local and global variables.

What is a local variable?

A local variable is a type of variable that exists inside a function or within a function. Local variables are initialized within the functions and end when the execution of the function is completed.

Let’s see an Example of a Local variable to understand this more.

Example of Local variable:

Function new_data(){
var a =10;
Var b=20; //These are the example of local variables
}
new_data();

What is a Global variable?

A global variable is the type of variable that exists outside the functions in the program. A global variable can be accessed anywhere in the program. Global variables start when the program starts executing and ends after the complete execution of an entire program.

Let’s understand the Global variable with an example.

var a = 5;
Var b =7;//These are Global variables
Function new_global(){

Var c = 12;//this is Local variable
}

Both Examples are from Javascript.

Local vs Global variables: Scope

What is a scope of a variable? The scope of a variable stands for the lifespan of a variable. Local and global variables both variable have different lifespans.

Scope of Local variable: Local variable declared inside a function so its lifespan only only exists inside a function. A local variable is initialized when the function starts executing and ends when the function execution process completes.

The scope of a local variable is only inside a function.

Scope of Global variable: Global variable can be accessed from anywhere and anytime throughout the whole program. The scope variable has a global scope.

Local and Global variables: Advantages and disadvantages

Each coin has two sides so do Local and global variables have. Local and global variables both have advantages and disadvantages.

Advantages and disadvantages of Local variables.

Advantages of local variable-

  • We can use variables with the same names under different functions which makes our code easier to understand for us.
  • Once the function execution completes, the memory of the local variable will be empty.
  • Variable inside function can’t be altered from outside so that will be locked with function only so the value will remain same.

Disadvantages of Local variable-

  • Data sharing with Local variables is prohibited.
  • The scope of the Local variable is limited inside the function.
  • Need common data multiple times and data sharing is not possible with local variables.

Advantages and disadvantages of Global variables.

Advantages of Global Variables-

  • Global variables can be accessed from anywhere and anytime.
  • We can use Global variables multiple times as we want which saves memory.
  • It is easier to change values with Global variables.
  • Global variables are reusable, which means Global variables need to be declared once and can be used multiple times, which is also good from developer perspectives.

Disadvantages of Global variables:

  • Global variables can be altered in large programs you may come across errors.
  • If you declare too many Global variables then they will be in the memory during the execution of an entire program which can make a lot of memory-related issues.
  • Global variables can be modified from anywhere and from any function so you have to be more careful to avoid errors

Key Difference table between Local and Global variables

Local Variable

Global Variable

Local variables are declared inside a function

Global variables are declared outside the function

Scope of Local variables lies inside a function.

The scope of Global variables lies in the entire program.

They can be accessed only in function

They can be accessed from anywhere in the program

They initialized when a function starts executing

They initialized when the Program starts executing

They end when execution of particular function completes

They end when the execution of whole program is completed. 

Does not allow data sharing

It allows data sharing

They can only be altered inside a function

They can be altered from anywhere in Program

Good for memory perspectives

Not good as Global variables stay in memory during the execution of the whole program

Any change in a local variable does not affect whole program

A slight change in Global variable can affect whole program

If we don’t initialize Local variable then it takes garbage value as default

If we don’t initialize Global variable then it takes undefined. 

Local variables can be declared multiple times with the same name, as they are inside functions

Global variables will be altered if we use the same name for two or more variables with the same name.

It is stored on the stack until you specify something

It is stored in fixed memory decided by the compiler.

Examples of Local and Global variables in Different Programming Languages.

You may be associated with or learning any programming language at this time so to understand local vs global variables more, let’s see their examples with different programming languages.

Also Check: Difference between Undefined and Notdefined

Example of Difference between Local and Global variables in Python

Local variables example in python

def func();

a=10;
b=20;
print(a);
print(b);
func();
print(a)

Output will be-

Output: 10
20
NameError: name 'a' is not defined

Global variable example in python

a=5;
def func();
a=10;
b=20;
print(a);
print(b);

func();
print(a)

The output will be:

Output:
10,
20,
5

Local vs Global variables: Examples in C language

Examples of Local and Global variables in C

include

#include <stdio.h>
// Global variables  
int x;  
int y;  
int Sum()  
{  

    return x + y;  
}  


void main()  
{  
    int Add, z=15;// This is Local variable  
    x = 5;  
    y = 10;  

    Add = Sum();  


    printf("The addition result is: %d\n",Add);  

              printf("%d\n", z);  
}  

FAQs related to the topic-

Que:1 What is the difference between global variables and local variables?

Ans: Local variable are variables within the function and global variables are variables which are declared outside the function.

Que: 2 How to access global variable if there is a local variable with same name in C/ C++?

Ans: In C language, We can access a global variable with a same name as local variable using keyword extern.

In C++, scope resolution operator (::) is very useful to access a global variable if we have a local variable with the same name.

Que: 3 How to fix local variable referenced before assignment error?

Ans: We can use Global keyword, Global variables have global scope so you can use this variable anywhere to avoid errors.

Leave a Reply

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