• Home
  • Testing
  • SAP
  • Web
  • Must Learn!
  • Big Data
  • Live Projects
  • AI
  • Blog

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.

In this tutorial, you will learn

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

Definition of Local Variable

A local variable is a type of variable declared within programming block or subroutines. It can only be used only inside that subroutine or code block in which they were declared. The local variable exists until the block of the function is in under execution. After that, it will be destroyed automatically.

Example of Local Variable

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

Here, 'a' and 'b' are local variables

Definition of Global Variable

Global variables are defined outside of a subroutine or function. The global variable will hold its value throughout the lifetime of a program. They can be accessed within any function defined for the program.

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.

Parameter Local Global
Scope It is declared inside a function. It is declared outside the function.
Value If it is not initialized, a garbage value is stored If it is not initialized zero is stored as default.
Lifetime It is created when the function starts execution and lost when the functions terminate. It is created before the program's global execution starts and lost when the program terminates.
Data sharing Data sharing is not possible as data of the local variable can be accessed by only one function. Data sharing is possible as multiple functions can access the same global variable.
Parameters Parameters passing is required for local variables to access the value in other function Parameters passing is not necessary for a global variable as it is visible throughout the program
Modification of variable value When the value of the local variable is modified in one function, the changes are not visible in another function. When the value of the global variable is modified in one function changes are visible in the rest of the program.
Accessed by Local variables can be accessed with the help of statements, inside a function in which they are declared. You can access global variables by any statement in the program.
Memory storage It is stored on the stack unless specified. It is stored on a fixed location decided by the compiler.

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.

 

YOU MIGHT LIKE: