What is Global Variable?
A variable is a name allocated to a storage area that can be controlled by the programme. A type of variable specifies the size and configuration of the memory of the variable.
The range of the value that can be inserted into a variable is calculated before inserting the value into it, as the type of the variable must be decided before inserting the value into it.
Scope of Variables
A variable's scope is literally a variable's lifespan. It is a code block in which a variable is valid or still alive.
Within the "a" function, we declare a variable "poo." The scope of that variable remains within that function, and cannot be used outside that function.
There are three places where we can utilize the variable in our programming.
- In a function or inside a block then it is said to be Local variables
- If used outside of all functions then it is named as Global variables
- If used in the function parameters definition then it is termed as formal parameters.
- The variable that is declared within a function is termed as local variable, while if the variable is declared outside the function is known as global variable.
- On the other hand, when the function has begun execution and is lost when the function finishes, global variables are created when execution begins and are lost when the programme ends. Local variables are created.
- The local variable does not include data sharing, whereas data sharing is given by the global variable.
- Local variables are stored on the stack, while the global variable is stored by the programmer at a fixed location.
- For local variables, parameters are needed while a global variable does not need to be transferred.
||Inside a function.
||Scope is outside the function.
||If the value is not initialized, will give garbage value.
||If the value is not initialized will give Zero by default.
||Gets created when the function starts execution and lost when the functions terminate.
||Gets created before the program's global execution starts and lost when the program terminates.
||We can access global variables throughout the program.
||On the stack.
||On a fixed location as assigned by the computer.
The Local Variable is specified in the programming block or subroutines as a form of variable declared. The local variable persists before the function's block is executed. It will be lost automatically after that.
Example of Local Variable
Here, 'x' and 'y' are local variables
A variable that is specified outside the function or block of the code is known as Global Variable. It has a global reach, which means it retains its relevance over the program's lifespan. Therefore, any feature specified within the programme can access it within the programme, unless it is shadowed.
Example of Global Variable
Here, 'x' and 'y' are global variables.
Advantages of using Global Variables
- The global variable can be accessed from all functions or modules in a programme.
- We only need to declare a single-time global variable outside the modules.
- It is used when the user needs to access the same data all over the program again and again.
Disadvantages of using Global Variables
- So many variables are declared global, then they stay in memory until the execution of the programme is completed. This can trigger the issue of Out of Memory.
- Any feature can change data. Any statement that is written in the programme will alter the global variable's value. In multi-tasking environments, this can offer unpredictable outcomes.
- We need to modify all the modules where they are named if global variables are discontinued due to code refactoring.
Advantages of using Local Variables
- The variables' values stay unchanged while the task is running is the basic meaning of the Local variable.
- If a single variable that is running concurrently is changed by many tasks, then the outcome can be unpredictable. However, declaring it as a local variable will solve it.
- We can assign the same name to the variables in different functions.
- As the function is executed, the memory of the variable is set free.
Disadvantages of Local Variables
- A local variable's debugging method is very tricky.
- Popular data needed to be transmitted regularly as data sharing between modules is not feasible.
- They have a spectrum that is very small.
What is more useful?
The equally essential local and global variable when writing a programme in any language. A large amount of the global variable, however, can occupy a huge memory. An unwanted transition to global variables is becoming difficult to define. It is also prudent to avoid announcing global variables that are unwanted.