The concept of function, a function is a code set that organizes code blocks with independent functions into a whole to make it have special functions
The role of the function, use the function It can enhance the reusability of code and improve the efficiency of program writing (recommended learning: Python video tutorial)
The use of functions, functions must be created before they can be used, this process It is called function definition. After the function is created, it can be used. The usage process is called function call
Function definition and call:
>>> def 函数名(形参1,形参2): # 定义 ... 函数体 函数名(实参1,实参2) #调用
The comment of the function is written in Below the function definition, use the """content""" method to place the mouse on the place where the pycharm function is called and press Ctrl to quickly view the annotation content of the function
The scope of the function parameters, defined inside the function They are called local variables. Variables outside the function are called global variables. The scope of local variables is limited to use inside the function.
>>> def test(a, b): ... print(a, b) ... >>> test(1, 2) 1 2 >>> print(a) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'a' is not defined 形参是外部是无法被调用的
For more Python-related technical articles, please visit the Python Tutorial column to learn!
The above is the detailed content of How to use python functions. For more information, please follow other related articles on the PHP Chinese website!