A global variable is one of the programming terms that stems from the division of variables.
Variables are divided into local and global, and local variables can be called internal variables. Variables created by an object or a function are usually local variables that can only be referenced internally and cannot be referenced by other objects or functions.
Global variables can be created either as an object function or anywhere in the program. Global variables are referenced by all objects or functions of the program. A local variable will be a null value when it is referenced by another object. But global variables don't happen.
Use attention
There are several main reasons for the existence of global variables:
1, the use of global variables will take up more memory (because its long life), but in today's high computer configuration, this should not be a problem, unless you use a global variable of huge objects, you can avoid it avoid.
2, using the global variable program to run faster (because the memory does not need to be redistributed), the same is not much faster.
3. For namespace pollution of local variables, this can be avoided without using too many variables.
4. When the global variable and the local variable have the same name, the local variable is used, and the global variable is masked.
5, you can also use extern to declare global variables outside the function, so that the scope of the global variable from the declaration to the end of the file.
In short, global variables can be used, but global variables should be used to make their names as easy to understand as possible, and not too short to avoid namespace pollution; avoid using global variables of huge objects. Declare a global variable as shown in the following example: Global y As String
Local variables are variables that are accessible only in a particular procedure or function in a program. Local variables are relative to global variables. In object-oriented languages ​​such as C++, C#, and Ruby, only local variables are generally used. Object-oriented programming is now commonly used in software development methods, so there is no need to consider whether it is a local variable or a global variable. When it comes to variables, they are often local variables.
In procedural languages ​​such as C, local variables can be renamed to global variables, but local variables mask global variables. When this variable is referenced within a function, a local variable of the same name is used instead of the global variable.
When applied to an object-oriented language such as Java, there may be cases where multiple local variables have duplicate names. For example, the formal parameter of a method is the same as the name of the class member. In this case, the formal parameter will block the class member. If you want to access the class member, you should use the this keyword.
Local variables and global variables
Variables defined in a subroutine are called local variables, and variables defined at the beginning of the program are called global variables.
The global variable scope is the entire program, and the local variable scope is the subroutine that defines the variable.
When a global variable has the same name as a local variable:
Within a subroutine that defines a local variable, the local variable works; in other places the global variable works.
Local variables can have duplicate names with global variables, but local variables mask global variables. To use global variables, you need to use ::. Referencing a variable in a function body uses a local variable of the same name instead of a global variable. For some compilers, multiple local variables of the same name can be defined in the same function body. For example, we can define a local variable i of the same name in both loops inside a function, and the scope of the local variable i is in that loop.
Specifically, the difference between global variables and local variables is as follows:
1. The scope is different: the scope of the global variable is the entire program, and the scope of the local variable is the current function or loop, etc.
2. Memory storage is different: global variables are stored in the global data area, local variables are stored in the stack area
3. Different lifetimes: The lifetime of a global variable is the same as that of the main program. It is destroyed by the destruction of the program. The local variable is inside the function or inside the loop. It does not exist with the exit or loop exit of the function.
4. Different ways of using: Global variables can be used in various parts of the program after declaration, but local variables can only be used locally. Local variables are used first in the function and then global variables are used.
One thing to note is that local variables cannot be assigned values ​​of the same name global variable.
[cpp] view plain copy#include"iostream"
#include"algorithm"
Using namespace std;
Int value = 1;
Int main(){
//test
Int value = value;
Cout "value" "endl;
Return 0;
}
Program output: -898993460
analyse as below:
1. Define a global variable value initialization value of 1, and define a local variable value inside the main function
2. Assign the local variable value to value = value inside the main function, because the local variable will mask the global variable, so the two values ​​in the assignment statement are local variables, because the value is not initialized, so the value is random.
3. So when the value is output, the value is a random value instead of 1
Waterproof Solar Panel Connectors,Diodes Solar Connector Mc4,Hot Sale Solar PV Cable Connector,PV Panel Wire MC4 Connector
Ruitian Cable CO.,LTD. , https://www.hbruitiancable.com