Difference Between Local and Global Variable

The main difference between local and global variable is that the local variable is declared inside a function while the global variable is declared outside the function in the program.

A variable is a name given to a memory location. It can be used to manipulate the values stored in that memory location throughout the program. There are two types of variables in programming languages such as C. They are local variables and global variables. A local variable is declared within a function, and it is accessible only by that function. On the other hand, a global variable is declared outside the function in the program. It is accessible by any statement in the entire program.

Key Areas Covered

1. What is a Local Variable
     – Definition, Functionality
2. What is a Global Variable
     – Definition, Functionality
3. Difference Between Local and Global Variable
     – Comparison of Key Differences

Key Terms

Global Variable, Local Variable, Memory Location

Difference Between Local and Global Variable - Comparison Summary

What is a Local Variable

A function or a method is a set of instructions that perform a specific task. A local variable is a variable declared inside a function. It is accessible only within that particular function. Other functions of the same program cannot access that variable. Assessing a local variable from some other function will give an error.

Program execution starts from the main method. When the main method calls for a function, the control is passed to that function from the main method. The local variable exists until the function executes. After completing the execution of that function, the control is passed back to the main method.  Therefore, the local variable only exists until the function executes.  After finishing the execution of the function, the local variable is destroyed.

Difference Between Local and Global Variable

Figure 1: Local Variables

In the above program, the ‘a’ and ‘b’ values are passed to the calculate_area function. A new variable called area is created inside that function. This variable is a local variable. It is accessible only within that function. It cannot be accessed inside the main method. 

What is a Global Variable

A global variable is a variable that is declared outside all functions inside the program. There can be multiple functions in a single program. All these functions can access these global variables. Therefore, a global variable is not very secure as the value can be changed by other functions. A global variable exists until completing the execution of the whole program.

Global variables are helpful when multiple functions operate on the same data. On the other hand, the value of a global variable is not reliable as it can be changed.  

Main Difference - Local vs Global Variable

Figure 2: Global variable

In the above program, ‘a’ and ‘b’ are global variables. Therefore, these variables are accessible within sum and increment_values functions. The sum function prints the summation of a and b. The increment_values function increments the ‘a’ and ‘b’ values by 1. When printing the ‘a’ and ‘b’ values in the main method, it will print the incremented values. Therefore, the global variables are accessible by all the functions in the program.  

Difference Between Local and Global Variable

Definition

Local variable is a variable that is declared inside a function of a computer program. Global variable is a variable that is declared outside the functions of a computer program. This is the basic difference between local and global variable.

Associability

Furthermore, while a local variable is accessible only within the function it is declared, a global variable is accessible by all the functions in the program.

Existence

Another important difference between local and global variable is their existence. A local variable exists until the function executes. Local variable is created when the function starts executing and is destroyed when the function execution is completed. On the other hand, a global variable remains in existence for the entire time the program is executing.

Reliability

Moreover, a local variable is more reliable and secure since the value cannot be changed by other functions. In contrary, a global variable is accessible by multiple functions. Therefore, its value can be changed.

Conclusion

Programming languages such as C has two types of variables called local variables and global variables. The difference between local and global variable is that the local variable is declared inside a function while a global variable is declared outside the function in the program.

Reference:

1. “C Functions.” Www.tutorialspoint.com, Tutorials Point, Available here.

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply