|
In computer programming, a global variable is a variable that is accessible in every scope. They are contrasted with local variables. See also scope. In computer programming in general, a scope is an enclosing context. ...
In computer science, a subroutine (function, procedure, or subprogram) is a sequence of code which performs a specific task, as part of a larger program, and is grouped as one, or more, statement blocks; such code is sometimes collected into software libraries. ...
In computer programming in general, a scope is an enclosing context. ...
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. This article needs cleanup. ...
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 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. Mutual exclusion (often abbreviated to mutex) algorithms are used in concurrent programming to avoid the concurrent use of un-shareable resources by pieces of computer code called critical sections. ...
Thread-safety is a computer programming concept applicable to multi-threaded programs. ...
An example of a global variable in C++: #include <iostream> int global(3); //This is the Global variable. void ChangeGlobal() { global = 5; //Reference to Global variable in a function. } int main() { cout << global << endl; //Reference to Global variable in another function. ChangeGlobal(); cout << global << endl; return 0; } As the variable was a global one, there were 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. DLL is an abbreviation which can commonly mean: Data link layer, a layer in the OSI network architecture model Dynamically Linked Library, a binary application library file format in Microsoft Windows and IBM OS/2 (see the Dynamic linking section of the Library (computer science) article) Doubly Linked List, a...
See also
William Wulf and Mary Shaw “Global Variable Considered Harmful” (ACM SIGPLAN Notices, 8, 2, pp. 23-34) sathish In computer science and mathematics, a variable is a symbol denoting a quantity or symbolic representation. ...
In computer science, there are several precise meanings of static variable, depending upon the use and context. ...
|