Home  >  Article  >  Backend Development  >  What are global variables

What are global variables

云罗郡主
云罗郡主Original
2018-12-19 14:31:1258090browse




What are global variables

one: What is a global variable?

Global variables are also called external variables. They are a term in programming. Object functions define variables externally and can be created anywhere in the program. Of course, they can also be programs. and object references.

2: The difference between global and local

1. The difference between global and local in Python [Recommended reading: What is a python crawler

The main difference between the two in Python is the scope. Global variables are declared in py and can be accessed globally, while Local variables are declared in one of the functions. If they exceed the calling scope, the program will be stuck, so they can only be called in the function in the program.

If the function and local names are consistent, other effects may occur. Under normal circumstances, we do not use it like this. For example:

def fun(x):  
    y=2  
    print("乘法的运行结果:",x*y)  
num1=1  
print("初始num1=",num1)  
fun(num1)  
print("y的值是:",y)

Result:

What are global variables

#The running result is an error. The reason may be that you are trying to access local variables, but the access is not in y.

2. The difference between global and local in C language

In C language, the main difference is that when calling a function, if it is a global variable, it is outside all codes, and the global variable The variable will not be stored on the stack, and the scope will be from the declaration to the end of the file. If the declaration is at the beginning of the code, the variable will be stored on the stack, and the scope will be the entire code block. If the parameter is in the function Header, the variable will affect the entire function and be stored on the stack.

The above is a complete introduction to what global variables are. If you want to know more about php getting started, please pay attention to the php Chinese website.





##

The above is the detailed content of What are global variables. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn