In computer programming, a global variable is a variable that is accessible in every scope (unless shadowed). Interaction mechanisms with global variables are called global environment (see also global state) mechanisms. The global environment paradigm is contrasted with the local environment paradigm, where all variables are local with no shared memory (and therefore all interactions can be reconducted to message passing).
They are usually considered bad practice precisely because of their non-locality: a global variable can potentially be modified from anywhere (unless they reside in protected memory or are otherwise rendered read-only), 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. For example, they can be used to avoid having to pass frequently-used variables continuously throughout several functions. Global variables also make it difficult to integrate modules because software written by others may use the same global names unless names are reserved by agreement, or by naming convention.
Global variables are used extensively to pass information between sections of code that do not share a caller/callee relation like concurrent threads and signal handlers. Languages (including C) where each file defines an implicit namespace eliminate most of the problems seen with languages with a global namespace though some problems may persist without proper encapsulation. Without proper locking (such as with a mutex), code using global variables will not be thread-safe except for read only values in protected memory.
Read more about Global Variable: C and C++, Environment Variables, Java: No Explicit Globals, PHP: Globals and Superglobals, Global-only and Global-by-default, Other Languages
Famous quotes containing the words global and/or variable:
“Much of what Mr. Wallace calls his global thinking is, no matter how you slice it, still globaloney. Mr. Wallaces warp of sense and his woof of nonsense is very tricky cloth out of which to cut the pattern of a post-war world.”
—Clare Boothe Luce (19031987)
“There is not so variable a thing in nature as a ladys head-dress.”
—Joseph Addison (16721719)