Detail the categories of variables by lifetimes including their respective advantages and disadvantages.

 We need to know about what is variable.?

A 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 that need to be stored inside that memory and the nature of operations that can be applied to that variable.

Scope of Variables

The scope of the variable is simply the life of a variable. It is a 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.

Categories of variables by lifetimes

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

Local Variable:-

local Variable is defined as a type of variable declared within programming blocks or subroutines. It can only be used inside the subroutine or code block in which it is declared. The local variable exists until the block of the function is 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

Global Variable:

Global Variable in the program is a variable defined outside the subroutine or function. It has a global scope means it holds its value throughout the lifetime of the program. Hence, it can be accessed throughout the program by any function defined within the program, unless it is shadowed.

Example:

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

Here, ‘a’ and ‘b’ are 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 variables a single time outside the modules.
  • It is ideally used for storing “constants” as it helps you keep 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
  • 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 are declared as global, then they remain in the memory till program execution is completed. This can cause Out of Memory issues.
  • 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 is required to pass repeatedly as data sharing is not possible between modules.
  • They have a very limited scope.

Comments

Popular posts from this blog

Create and outline the timeline of History of Operating Systems.