Difference between Local Vs. Global variable

11:53 am
January 5, 2023
cogent infotech
Application Development
Cogent University
Fortune 500

Difference between local Vs. global variable

A variable is a name we assign to a memory location to store values in a computer program. The name given should follow the programming semantics such as a,b,c,x,y,z, sub, div, total, avg, etc.

What is a variable?

A variable is a value that can change, dependent on certain conditions or information being passed through a program. Variables are commonly used as a memory to store data to be referenced or manipulated later when the need arises. By referring to the term variable, in computer programming, most commonly, we refer to a memory location. Or, in general, we can say a variable is a named unit of data assigned a value. Even by changing the deal, the name remains the same. 

What’s the difference between local Vs Global Variables?

Global Variable:

A global variable is a variable that exists outside the function or the subroutine. It has an international scope, meaning it exists until the program runs. It is accessible by any process within the entire program. It’s like a master file that any department can retrieve.

Global Variable Example:

int a =4;

int b=5;

public int add(){

return a+b;


Here, ‘a’ and ‘b’ are global variables.

Local Variable:

A local Variable is a type of variable constrained within a programming block. It has no existence outside the boundaries of that specific function, after which it automatically destroys. One can only use it within that particular function or the code block it has been declared. This local variable exists till the position exists.

Local Variable Example

public int add(){

int a =4;

int b=5;

return a+b;


‘a’ and ‘b’ are local variables here in this function.


Some of the primary differences between the local and the global variables are:

Benefits: Local Variable

  • It prevents any accidental alteration of data. The variable is restricted inside a particular block and used within the code block, negating any side effects.
  • They consume memory for a limited period, for the time the function runs or the block is executed.
  • Local variables are auto-deleted with the lifespan of the function.
  • The same name can be assigned for local variables within different functions, as each has a separate local variable.
  • They guarantee that the variables’ value remains the same during the running of a task.

Benefits: Global Variable

  • We can retrieve the global variables from all the functions of a program.
  • We need to declare the global variable only once in a program.
  • Global variables are helpful when multiple functions retrieve similar data simultaneously.
  • Generally, we use global variables for storing ‘constants.’
  • When specific changes are required in the entire program, it becomes more accessible to apply via global variables.
  • Accessibility across the program becomes easier while using a global variable.

Disadvantages: Local Variables

  • Debugging process is complicated.
  • Limited scope.
  • Data needs to be updated within each block repeatedly.

Disadvantages: Global Variables

  • The more considerable number of global variables accounts for program errors.
  • All variables are stored till the whole program is running, which can result in a memory shortage.
  • Code refactoring is critical, and we must restructure each module if global variables are halted.



Both local variables and global variables are required while programming. However, declaring loads of global variables is unsolicited due to its unwanted changes in the worldwide program.

Discovering the location of the difference can be a cumbersome process. Avoid the creation of any unnecessary global variables.

 Cogent Infotech offers great help to new coders writing the codes for the first time. When you begin your programming journey, feel free to connect with our expert consultants.


This is some text inside of a div block.

What’s a Rich Text element?

The rich text element allows you to create and format headings, paragraphs, blockquotes, images, and video all in one place instead of having to add and format them individually. Just double-click and easily create content.

Static and dynamic content editing

A rich text element can be used with static or dynamic content. For static content, just drop it into any page and begin editing. For dynamic content, add a rich text field to any collection and then connect a rich text element to that field in the settings panel. Voila!

How to customize formatting for each rich text

Headings, paragraphs, blockquotes, figures, images, and figure captions can all be styled after a class is added to the rich text element using the "When inside of" nested selector system.

Related Resources