Function is one of the core content of Python programming. This article first introduces you to what functions are, then introduces the function definition and calling methods in detail. Finally, we use a large number of instance codes to detail the scope of variables and global statements related to them.
1. What is a function?
In many cases, statements in a Python program are organized into functions. In layman's terms, a function is a statement group that completes a specific function. This group of statements can be used as a unit and given a name to it. In this way, we can execute this function multiple times in different places of the program through the function name), but do not need to write these statements repeatedly in all places. In addition, each time you use a function, you can provide different parameters as input to process different data. After the function is processed, you can also feedback the corresponding results to us.
Some functions are compiled by the user, which is usually called user-defined functions. In addition, the system also comes with some functions, and some third-party functions, such as some functions compiled by other programmers, we call it a pre-defined Python function, which can be used directly by users of these ready-made functions.
Ii. Why are functions used?
We use functions mainly for two reasons: first, to reduce programming difficulty, we usually break down a complex big problem into a series of simpler small problems, then we divide small problems into smaller ones. when the problem is refined to simple enough, we can divide and conquer it. At this time, we can use functions to deal with specific problems. Once small problems are solved, the major problems will be solved. Second, code reuse. The defined functions can be used in multiple locations of a program, or in multiple programs. In addition, we can put functions in a module for other programmers to use. At the same time, we can also use functions defined by other programmers. This avoids repetitive work and provides work efficiency.
3. Define and call Functions
When we define a function, def statements are usually used. The syntax format is as follows:
Def function name parameter list): function body |
The function name can be any valid Python identifier. The parameter list is the value passed to the function when it is called. It can contain multiple, one, or zero parameters, when multiple parameters exist, each parameter is separated by a comma. Parentheses are essential and cannot be left blank even if no parameter exists. The function body is the code that is executed every time a function is called, it can be composed of one or more statements. The function must be indented. In addition, beginners often forget the colon following the parentheses, which leads to syntax errors.
Here we will introduce the formal parameters and actual parameters. when defining a function, the variable names in parentheses after the function name are called "formal parameters" or "form Parameters". When calling a function, the variable name in parentheses after the function name is called "actual parameter" or "real parameter" for short ".
Please refer to the following function definition. The defined function will add 1 to the value passed to it, and then return the added value to the caller:
def add1(x):x = x + 1return x |
The return statement ends the function call and returns the result to the caller. However, for a function, this statement is optional and can appear anywhere in the function body. If there is no return statement, the function returns the control to the caller at the end of the function body, which is equivalent to the "process" in other programming languages ". In this example, the return statement is to pass the value of variable x to the caller. Test the function in interactive mode, as shown in:
Figure1Function example