What is a Variable?

Variable is a name assign to a storage area that the program can manipulate. A variable type determines the size and layout of the variable’s memory. It also determines the range of values which need to be stored inside that memory and nature of operations that can be applied to that variable.

Scope of Variables

The scope of the variable is simply lifetime of a variable. It is block of code under which a variable is applicable or alive. For example:

function foo(){ var x; }

You declare a variable “x” inside a function “foo.” The scope of that variable remains inside that function it can’t be used outside of that function. There are three places where variables you can declare variable programming language:

Inside a function or a block:     Local variables
Outside of all functions:     Global variables
In the definition of function parameters: Formal parameters

Example of Local Variable

public int add(){ int a =4; int b=5; return a+b; }

Here, ‘a’ and ‘b’ are local variables Example:

int a =4; int b=5; public int add(){ return a+b; }

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

Local Variable vs Global Variables

Here, are some fundamental differences between Local and Global variables.

Advantages of using Global variables

You can access the global variable from all the functions or modules in a program You only require to declare global variable single time outside the modules. It is ideally used for storing “constants” as it helps you keep the consistency. A Global variable is useful when multiple functions are accessing the same data.

Advantages of using Local Variables

The use of local variables offer a guarantee that the values of variables will remain intact while the task is running If several tasks change a single variable that is running simultaneously, then the result may be unpredictable. But declaring it as local variable solves this issue as each task will create its own instance of the local variable. You can give local variables the same name in different functions because they are only recognized by the function they are declared in. Local variables are deleted as soon as any function is over and release the memory space which it occupies.

Disadvantages of using Global Variables

Too many variables declared as global, then they remain in the memory till program execution is completed. This can cause of Out of Memory issue. Data can be modified by any function. Any statement written in the program can change the value of the global variable. This may give unpredictable results in multi-tasking environments. If global variables are discontinued due to code refactoring, you will need to change all the modules where they are called.

Disadvantages of using Local Variables

The debugging process of a local variable is quite tricky. Common data required to pass repeatedly as data sharing is not possible between modules. They have a very limited scope.

What is more useful?

The local and global variable equally important while writing a program in any language. However, a large number of the global variable may occupy a huge memory. An undesirable change to global variables is become tough to identify. Therefore, it is advisable to avoid declaring unwanted global variables.