Detailed explanation of python calling functions


1. The order in which functions are defined and called

After the function is defined, it will not be executed automatically. Only after it is called, the function will be executed and the corresponding results will be obtained. But one key point we need to pay attention to in Python is that Python does not allow forward references, that is, the function is not allowed to be called before the function is defined.

For example:

print plus(1,2)
def plus(a,b):  
    return a+b  

The running result is:

NameError: name 'plus' is not defined  

It can be seen from the error result that the function named plus has not been defined yet (although we will define it later). So when we call a function, we must make sure that the function is defined before the call.

2. Use parameters correctly

When we call a function, we need to correctly call the function name and parameters.

For example, we define an addition function:

def plus(a,b):  
    return a+b  

When we call the plus() function, if the parameter type passed in is incorrect, a TypeError error will be reported. And sometimes if the parameter type we pass in is not the specified type, even if the calling function does not report an error, a logic error will occur.

For example:

# 定义plus函数,作用是求两个正整数之和  
def plus(a,b):  
    return a+b
# 调用plus函数,参数类型为'1','2'  
print(plus('1','2'))  

Output result:

12  

Although the program in the above example did not report an error, the result was not in line with our expectations because our original intention was to call the plus() bold style function to add two integers. But if we pass in a string type value, the result is the concatenation of two strings. So this example tells us that we must pay attention to the type of parameters passed in. When we pass in normal type parameters and the number of passed parameters is inconsistent, a TypeError will also be reported.

For example:

# 定义plus函数,作用是求两个正整数之和  
def plus(a,b):  
    return a+b
# 调用plus函数,参数为1,2,3  
print(plus(1,2,3))  

Error reported:

TypeError: plus() takes 2 positional arguments but 3 were given  

The reason for the error shows that because the plus() function allows only 2 parameters, but 3 parameters were passed in when calling, the program reported an error.

3. Calls between functions

3.1 The program code for the first case is as follows:

def x(f):  
    def y():  
        print(1)  
    return y
def f():  
    print(2)
x(f)  

operation result:

无结果

Analysis: Because f in x(f) in line 9 does not have parentheses and f is just an ordinary parameter, the program only calls and executes the x(f) function and does not call and execute the f() function. Although the y() function is also defined in the x(f) function, the y() function is not called and executed in the x(f) function, but return y is executed. The y variable has no value, so the return value of the entire program is no result.

3.2 The program code for the second case is as follows:

def x(f):  
    def y():  
        print(1)  
    return y
def f():  
    print(2)
x(f())  

operation result:

2  

Analysis: Because f() in x(f()) in line 9 has parentheses, the program first calls and executes the f() function and outputs 2 first. Then 2 is passed into the x(f) function as a parameter, and the program begins to call and execute the x(f) function. Although the y() function is also defined in the x(f) function, the y() function is not called and executed in the x(f) function, but return y is executed. The y variable has no value, so calling the x(f) function does not produce any results. So the program ends up outputting only 2.

3.3 The program code for the third case is as follows:

def x(f):  
    def y():  
        print(1)  
    return y()
def f():  
    print(2)
x(f)  

operation result:

1

Analysis: Because f in x(f) in line 9 does not have parentheses and f is just an ordinary parameter, the program only calls and executes the x(f) function and does not call and execute the f() function. The y() function is also defined in the x(f) function, and the return is the y() function. Therefore, the program calls and executes the y() function, and the output result is 1.

3.4 The fourth situation

def x(f):  
    def y():  
        print(1)  
    return y()
def f():  
    print(2)
x(f())

operation result:

2  
1  

Analysis: Because f() in x(f()) in line 9 has parentheses, the program calls and executes the f() function and outputs 2 first. Then 2 is passed into the x(f) function as a parameter, and the program begins to call and execute the x(f) function. The y() function is also defined in the x(f) function, and the return is the y() function. Therefore, the program also calls and executes the y() function and outputs 1. Finally, the program outputs 2 and 1 successively.

3.5 The program code for the fifth case is as follows:

def x(f):  
    def y():  
        print(1)  
        print(f())  
    return y()
def f():  
    #print(2)  
    return 2
x(f)  

operation result:

1  
2  

Analysis: Because f in x(f) in line 9 does not have parentheses and f is just an ordinary parameter, the program just calls and executes the x(f) function first. The y() function is also defined in the x(f) function, and the return value is the y() function, so the program will call and execute the y() function. In the y() function, the print(1) statement is first executed, outputting 1, and then the print(f()) statement is executed, so the program will also call and execute the f() function at this time, outputting 2. Finally, the program outputs 1 and 2 successively.

Okay, that’s it for today. Welcome to like and collect!

Guess you like

Origin blog.csdn.net/Rocky006/article/details/132963993