Blog
5 BEST Sauce Labs Competitors & Alternatives (2021)
Sauce Labs is an application that allows you to test your mobile applications and website across...
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
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:
Local Variable is defined as a type of variable declared within programming block 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
A 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.
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. |
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.
Sauce Labs is an application that allows you to test your mobile applications and website across...
Duplicate file finders are tools that help you to find and remove repeating files in folders and...
What is the VI editor? The VI editor is the most popular and classic text editor in the Linux...
Generally, in a large organization, there are multiple, separate teams to manage and run jobs in...
Video grabbers are tools to store videos in numerous formats, including MP3 and MP4. These...
Photo viewer is computer software that can display stored pictures. These tools can handle many...