After the previous function learning, we found that the function will not be executed directly without being called. After previous function calls, we found that the running results are all printed out by print() in the function body. However, sometimes in order to facilitate the function to participate in the secondary operation, we let the function body not output any results, but treat the function itself as a result. The way of outputting this result can be understood as returning the result of the function, python uses the return keyword to return. Let's compare the results of several different function calls.
1, Comparison of output modes of function
1. print the function running result directly: call the function name directly and pass the parameter.
def func1(a, b): res = a + b print(res) func1(4, 9) Return result: 13
2. Print the function without return value and output code block. You need to print the function as a variable.
def func2(a, b): res = a + b print(func2(4, 9)) Return result: None
3. The function with return printed is the same as above. It also outputs the function as a variable.
def func3(a, b): res = a + b return res # print(a) # The code after return will not be executed print(func3(4, 9)) Return result: 13
Compared with the above three forms of functions, if we want to use the result of the function for operation, the first case cannot be realized, for example
func1(4, 9) * 3 Return result: TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'
The second case itself is None, so ignore it. Let's try the third case again
print(func3(4, 9) * 3) Return result: 39
As can be seen from the above results, functions with return values are very convenient to use and can be directly used as variables.
2, Function of return
At the same time, return also has the function of ending the function code block. The next line of statements after return will not be executed.
Note: functions with return values generally call the function name directly and do not execute any results. The results will be returned only after they are assigned to variables. If a function does not have a return statement, it actually has an implicit statement. The return value is None and the type is' None Type '. print is printed on the console, while return takes the following part as the return value. "
Let's take a look at some special features of return.
1. You can return multiple results
def func3(a, b): res1 = a + b res2 = a - b return res1, res2 print(func3(4, 9)) Return result: 13 -5
2. A function can have multiple return s, but only the first one will be executed
def func3(a, b): res1 = a + b res2 = a - b return res1 return res2 print(func3(4, 9)) Return result: 13
3. Functions without return return return NoneType
def func3(a, b): res1 = a + b res2 = a - b print(type(func2(4, 9))) Return result:<class 'NoneType'>
3, Help function
This is a supplementary knowledge point. We can use help() function to output text prompts in development documents when we don't know other usage of the parameter passing function.
help(print)import os #File directory operation module os.mkdir('123') help(os.mkdir)
Return result:
Help on built-in function print in module builtins: print(...) print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream. Help on built-in function mkdir in module nt: mkdir(path, mode=511, *, dir_fd=None) Create a directory. If dir_fd is not None, it should be a file descriptor open to a directory, and path should be relative; path will then be relative to that directory. dir_fd may not be implemented on your platform. If it is unavailable, using it will raise a NotImplementedError. The mode argument is ignored on Windows.
The above is about the Python function return value type and help function. If novices don't understand it, they can go to the Python self-study network to see the corresponding video explanation, which will be more detailed.
Source: www.wakey.com.cn/document-func-return.html