Advantages and Disadvantages of Local and Global Variable

Question: Advantages and Disadvantages of Local and Global Variable.


Advantages and Disadvantages of Local Variable.
Advantages of using Local Variables Disadvantages 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 The debugging process of a local variable is quite tricky.
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. Common data required to pass repeatedly as data sharing is not possible between modules.
You can give local variables the same name in different functions because they are only recognized by the function they are declared in. They have a very limited scope.
Local variables are deleted as soon as any function is over and release the memory space which it occupies.



Advantages and Disadvantages of Global Variable.
Advantages of using Global variables Disadvantages of using Global Variables
You can access the global variable from all the functions or modules in a program 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.
You only require to declare global variable single time outside the modules. 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.
It is ideally used for storing "constants" as it helps you keep the consistency. If global variables are discontinued due to code refactoring, you will need to change all the modules where they are called.
A Global variable is useful when multiple functions are accessing the same data.

Advantages and Disadvantages of Local and Global Variable

Download the Android app to get all Government Job Notifications on your Mobile.
Download Now
Important: Please always Check and Confirm the above details with the official Advertisement / Notification.
Previous Post Next Post