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:
“However global I strove to become in my thinking over the past twenty years, my sons kept me rooted to an utterly pedestrian view, intimately involved with the most inspiring and fractious passages in human development. However unconsciously by now, motherhood informs every thought I have, influencing everything I do. More than any other part of my life, being a mother taught me what it means to be human.”
—Mary Kay Blakely (20th century)
“Walked forth to ease my pain
Along the shore of silver streaming Thames,
Whose rutty bank, the which his river hems,
Was painted all with variable flowers,”
—Edmund Spenser (1552?1599)