GLOBAL VARIABLE
In computer programming, a 'global variable' is a variable that is accessible in every scope. Interaction mechanism 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 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. 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 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.
An example of a global variable in C++:
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.
★ Variables
★
★ Static variable
★
★ External variable
★ Singleton pattern
★ William Wulf and Mary Shaw, “Global Variable Considered Harmful”, ACM SIGPLAN Notices, volume 8, issue 2, 1973 February, pp. 28-34.
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. 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 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.
An example of a global variable in C++:
#include
int global = 3; // This is the global variable.
void ChangeGlobal()
{
global = 5; // Reference to global variable in a function.
}
int main()
{
std::cout << global << endl; // Reference to global variable in another function.
ChangeGlobal();
std::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.
| Contents |
| See also |
| References |
See also
★ Variables
★
★ Static variable
★
★ External variable
★ Singleton pattern
References
★ William Wulf and Mary Shaw, “Global Variable Considered Harmful”, ACM SIGPLAN Notices, volume 8, issue 2, 1973 February, pp. 28-34.
This article provided by Wikipedia. To edit the contents of this article, click here for original source.
psst.. try this: add to faves
Featured Companies
| Great Time Travel | |
| Sheraton Vancouver Airport Hotel | |
| Optimum 1 Travel | |
| Aquaworld Cancun |

العربية
中国
Français
Deutsch
Ελληνική
हिन्दी
Italiano
日本語
Português
Русский
Español