Difference between Local variable and Global Variable
Variables in any programming language have a crucial role. Variables are classified into Global variables and Local variables based on their scope. The main difference between Global and local variables is that global variables can be accessed globally in the entire program, whereas local variables can be accessed only within the function or block in which they are defined. In this topic, we will first understand what are the variables and scope, along with local variables, global variables, and then differences between both the variables.
What is a Variable?
A variable is a name given to a memory location to store values in a computer program. It is used to store information that can be referenced and manipulated in a program.
We can choose any name for the variable, but it must follow the programming semantics. Such as it can be, a, b, x, y, z, sub, div, total, avg, etc.
Let's say there are two values, 10 and 20, that we want to store and use in our program. For this, we need to use a variable, and we will do the below steps:
As we can see in the above image, there are two memory slots, 001 and 002, and we have given names to these locations as A and B. A is containing 10, and B is containing 20.
Different programming languages have different ways to declare the variable. For example, in C language, we can declare the variable in the following manner:
Syntax: (Variable declaration syntax in C language)
Scope of Variable
Each variable is defined and can be used within its scope and determines that wherein the program this variable is available to use. The scope means the lifetime of that variable. It means the variable can only be accessed or visible within its scope.
The scope of variables can be defined with their declaration, and variables are declared mainly in two ways:
What is a Global Variable?
In the above example, a and b are the global variables.
Advantages of Global Variable
Disadvantages of Global Variable
What is a Local Variable?
In the above example, we have declared x and y two variables inside the main function. Hence these are local variables.
Advantages of Local Variable
Disadvantages of Local Variables
Comparison Chart Between Global Variable and Local Variable
Examples to understand differences between Local and Global Variable
Now let's understand examples in different programming languages to better understand the difference between local and global variables.
Local vs Global in C
The addition result is: 120 The Multiplication result is: 200 30
As we can see in the above program, we have taken a and b global variables that are being accessed in different functions such as Add() and main(). Whereas there are also local variables such as c, d, Ans1, and Ans2, which are being accessed by those functions only in which they are declared.
If we try to use c and d variables outside the Mul() function, they will be used as new variables. As we have shown by taking c in the main() function also, it is treated as a new variable.
Local Vs. Global in Python
Hey, I am a Local Variable!, I can be used within this block only in the program. Hey, I am Global Variable!, I can be used everywhere in the program.
In the above program, we have taken one global variable v1 and one local variable v2. Since v1 is global, it can be easily accessed in any function, and v2 is local; it is used only within its declared function. But if we try to use v1 in func1, it will give an error. Let's see the below example:
If we try accessing v1, it can easily be accessed in fun1 and func2. But if we try to access v2 outside its function, which means in func2, it will give the runtime error. We will get the below output after executing the above code:
NameError: global name 'v2' is not defined
Hey, I am Local Variable!, I can be used within this block only in the program. Hey, I am Global Variable!, I can be used everywhere in the program. Hey, I am Global Variable!, I can be used everywhere in the program.
Local Vs. Global Variable in Java
In Java, there is no concept of global variables; since Java is an Object-oriented programming language, everything is a part of the Class. But if we want to make a variable globally accessible, we can make it static by using a static Keyword.
Value of non-static variable is: 20 Value of static variable is:10
In the above program, we have used one local variable or non-static variable and one static variable. The local variable can be accessed by using the object of the Demo class, whereas the static variable can be accessed using the name of the class.
Next TopicDifference between