Function
Call Function:
Python has built in a lot of useful functions that we can call directly. To know the details of the function, you can view the documentation for the Python website: https://docs.python.org/3/library/functions.html. You can also view the details of a function on the interactive command line through Help (function name).
When calling a function, the number of arguments passed in and the type of arguments passed in are not typeerror errors, and an error message is given.
The function name is actually a reference to a function object, and it is possible to assign the function name to a variable, which is equivalent to giving the function an "alias".
To define a function:
In Python, you define a function to use a def statement, write out the function name, parentheses, the arguments in parentheses, and a colon, and : then, in the indent block, write the function body, and the return value of the function is returned with a return statement.
Note that when the statement inside the function body executes, the function executes and returns the result as soon as it is executed return . Therefore, it is possible to implement very complex logic within a function through conditional judgments and loops.
If there is no return statement, the result is returned after the function is executed, except for the result None . return Nonecan be abbreviated to return .
Null function: If you want to define an empty function that does nothing, you can use a pass statement. Easy to add code later.
Parameter check: When calling a function, if the number of arguments is incorrect, the Python interpreter will automatically check it out and throwTypeError;但是如果参数类型不对,Python解释器就无法帮我们检查。数据类型检查可以用内置函数isinstance()实现。
Returns multiple values: The Python function returns a tuple, but it is more convenient to write.
Python Learning Note (c) function