Local and Global Variable

Local and Global Variable

Local and global variable:

In computer science, a local variable is a variable that is given local scope. Local variable references in the function or block in which it is declared override the same variable name in the references scope. In programming languages with only two levels of visibility. A local variable is contrasted with a global variable. Local variables are declared inside a function and can be used only inside that function.

On the other hand, many ALGOL-derived languages allow any number of nested levels of visibility with private variables, functions, constants, and types hidden within them, either by nested blocks or nested function. Local variables are fundamental to procedural programming and more generally modular.

Programming variables of local scope are used to avoid issues with side-effects that can occur with global variables. Global variables are not much reliable as their value can be changed by any function present in the program. A Global variable is helpful in situations where multiple functions are accessing the same data.

In computer programming a global variable is a variable with global scope, meaning that it is visible throughout the program unless shadowed. The set of all global variables is known as the global environment or global state. In compiled languages, a global variable is generally static variables whose extent ii the entire runtime of the program through in interpreted languages, global variables are generally dynamically allocated when declared. Since they are not known ahead of time.

A local variable is destroyed when the control of the program exit out of the block in which local variable is declared. However, a global variable is destroyed when the entire program is terminated.