I keep seeing things like "the variable is global" or "global variables" when I am reading tech forums.
Could anyone explain what this means??
Five answers:
ProudAirman1
2014-03-10 14:02:53 UTC
Whether a variable is global or not is a matter of scope.
Global variables will be accessible to any function in your program, whereas local variables will only be available to the function in which they are declared(unless you pass them as a reference parameter to another function.)
As a new programmer, its best to stay away from global variables(which are declared above the main function).
New programmers tend to declare global variables to get around information flow problems, but its better to learn to pass information in a controlled and predictable manner.
You may also be interested in learning about static variables.
?
2014-03-10 20:32:34 UTC
To understand what global variable means, lets understand what is a scope ?
Scope means a limit of something. In our case a variable. So there are variable which have different scopes. Some variable's scope(limit) can be up to certain thing, beyond which they become invalid. But certain variables have a scope without limit.
Now lets consider an example, we have two variables, L (local) and G (Global). Now, if we have one function/method, than any variables declared within it are local variables to that function/method, so there scope becomes local to that function and they become invalid outside that function. Any operation done on this variable exists only till the function/method is being executed. After that the variable does not exist.
And if we do not enclose a variable inside any function/method, then there scope is global. Means they are valid in entire program. Any function/method within that program can access this variable.The state of the variable can be changed at any point of time in program.
amania_r
2014-03-10 19:50:08 UTC
The scope of most variables used is the function the variable is declared in. These local variables have no meaning outside. A global variable can be accessed from any function in the program.
Often used to store configuration values.
Scootaloo
2014-03-10 19:14:00 UTC
A variable that affects the entire system versus one that affects a single running process.
Example: $USER is a global variable containing the currently logged-in user on a Linux machine. $INPUT might be a local variable that I use in a single script that has no bearing outside of that script.
?
2014-03-10 20:29:04 UTC
lets take an example of C programming language.
If you have more than 1 functions in your program and you want to use a particular variable(s) in all of that , so it is a better idea to identify it/them a "global variable" at the starting outside the brackets {} rather than creating them again and again as a "local variable" inside the brackets {}. lets take a simple example
#include
// Global variables
int A = 5;
int B = 7;
int Add()
{
return A + B;
}
int Multiply()
{
return A * B;
}
int main()
{
int sum; // Local variable
int prod; // Local variable
sum = Add();
printf("%d\n",sum);
prod = Multiply();
printf("%d\n",prod);
return 0;
}
Here
A & B are Global variables
sum & prod are local variables
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.