In computer programming, a global variable is a variable that does not belong to any subroutine in particular and can therefore can be accessed from any context in a program. They are contrasted with local variables. See also, scope.
They are usually considered bad practice precisely because of their nonlocality: a global variable can potentially be modified from anywhere, and any part of the program may depend on it. A global variable therefore has an unlimited potential for creating mutual dependencies, and adding mutual dependencies increases complexity. See Action at a distance. However, in a few cases, global variables can be suitable for use. They can be used to avoid having to pass frequently-used variables continuously throughout several functions, for example.
Compare this with a constant, which can be seen as a read-only global variable (even though this use of variable is an oxymoron). Because a constant, by virtue of its constancy, can only be "modified" (defined) at one point in the program, all dependencies on a constant are one-way.
Multiple blocks of code accessing a globalvariable are dependent on each other (because of their mutual dependence on the variable), yet the relationship between them is indirect and even further-removed.
In addition, the state of an object dependent on a globalvariable is dependent on the state of the globalvariable, which is not under its control.
Removing the ability to access the internal state of a globalvariable does not make it less global, but it does reduce the coupling, and it also reduces the coupling between separate blocks of code that are dependent on the globalvariable.
In computer programming, a globalvariable is a variable that does not belong to any subroutine in particular and can therefore can be accessed from any context in a program.
They are usually considered bad practice precisely because of their nonlocality: a globalvariable can potentially be modified from anywhere, and any part of the program may depend on it.
Compare this with a constant, which can be seen as a read-only globalvariable (even though this use of variable is an oxymoron).