Differences between Local and Global Variable
Define Global Variable
- Global variables are those that may be accessible worldwide across a programme and are defined outside of any functions or blocks.
- It may be accessed by any function in the application.
- A global variable's value can be changed after it has been defined for usage with different functions.
- Until the programme is executed, the global variable has a lifespan.
- For these variables, the compiler defines fixed memory regions that are not automatically cleaned up.
- Global variables are frequently used in programming because they are helpful when several functions need to access the same data.
Advantages:
- Gains from Global Variables The functions in the programme can all access global variables.
- There is only one declaration required.
- This is especially useful if every function has access to the same data.
Disadvantages:
- Problems with a Global Variable an unintentional value change can occur in a global variable since it can be utilised by any computer function.
- If we employ a lot of global variables, there is a great chance that the programme may generate errors.
Define Local Variables
- Any variables that are declared within or within a function block are considered local variables.
- These variables can only be accessible within the specified function.
- The local variable remains in effect until the function is complete since it only exists while its function is active.
- Once the function has done running, local variables are deleted and no longer exist outside the function.
- Local variables have a constrained scope since they are kept on the stack, which is dynamic in nature and immediately cleans up the data it holds.
- However, by declaring the variable static using the "static" keyword, we are able to keep the local variable's value.
Advantages:
- Benefits of Local Variables Due to the fact that a local variable's name is unique inside a single function, it can be utilised in many functions.
- Only a brief amount of time is spent utilising the local variables' memory while the function is executed; after that, the same memory address can be used again.
Disadvantages:
- Local variables' negative aspects the scope of the local variable is limited to the scope of its function and cannot be used by other functions.
Difference between Local and Global Variable
Global variable | Local variable |
Outside of every function block, global variables are declared. | The declaration of local variables occurs within a function block. |
The program's scope does not change. | They have a narrow scope that is solely applicable to the stated function. |
No matter where a global variable is utilised, every change to it affects the programme as a whole | Changes to the local variable have no effect on other parts of the programme. |
A global variable exists for the length of the program's execution. | A local variable is generated when the function is performed and deleted when it is done. |
It is reachable from all programme functions wherever in the programme. | No other functions, only the defined function statements, may access it. |
If the global variable is not initialised, it defaults to taking a value of zero. If | the local variable is not initialised, the trash value serves as its default value. |
Global variables are stored in the memory's data section. | Local variables are stored in memory as a stack. |
It is not possible to declare several variables with the same name. | We can define numerous variables with the same name in different functions. |