- Global variable
computer programming, a global variable is a variable that is accessible in every scope. Interaction mechanisms with global variables are called global environment (see also global state) mechanisms. The global environment paradigm is contrasted with the local environmentparadigm, 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 nonlocality: a global variable can potentially be modified from anywhere, (unless they reside in
protected memory) 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 are used extensively to pass information between sections of code that don't share a caller/callee relation like concurrent threads and signal handlers. Languages where each file defines an implicit namespace eliminate most of the problems seen with languages with a global
namespacethough some problems may persist without proper encapsulation. Without proper locking (such as with a mutex), code using global variables will not be thread-safeexcept for read only values in protected memory.
An example of a global variable in
C++:As the variable is a global one, there is no need to pass it as a parameter to use it in a function besides main. The global variable belongs to every function in the program.
The output will be: 3 5
The use of global variables makes software harder to read and understand. Since any code anywhere in the program can change the value of the variable at any time, understanding the use of the variable may entail understanding a large portion of the program. They make separating code into reusable libraries more difficult because many systems (such as
DLLs) don't directly support viewing global variables in other modules. They can lead to problems of naming because a global variable makes a name dangerous to use for any other local or object scope variable. A local variable of the same name can shield the global variable from access, again leading to harder to understand code. The setting of a global variable can create side effects that are hard to understand and predict. The use of globals make it more difficult to isolate units of code for purposes of unit testing, thus they can directly contribute to lowering the quality of the code.
If a variable in a function is not defined, then it is automatically considered as a global variable. The current value of this variable is used in the function. Functions can be invoked with less input or output parameters.
Some languages, like Java, don't have global variables. In Java, all variables that are not local variables are fields of a class. Hence all variables are in the scope of either a class or a method. In Java, static fields (aka class variables) exist independently of any instances of the class and one copy is shared among all instances; hence static fields are used for many of the same purposes as global variables in other languages because of their similar "sharing" behavior.
William Wulfand Mary Shaw, “Global Variable Considered Harmful”, ACM SIGPLAN Notices, volume 8, issue 2, 1973 February, pp. 28-34.
Wikimedia Foundation. 2010.