first, the function
1. Calling functions
A function name is a reference to a function object, and it is possible to assign a function name to a variable, which is equivalent to an "alias" for the function.
# variable a points to the ABS function # so you can also call the ABS function by a 1
2. Defining functions
Define the function with a DEF statement, write down the function name, parentheses, the arguments in parentheses, and the colon: and then write the function body in the indent block clock.
For example, define a my_abs function that evaluates to an absolute value:
def my_abs (x): if x>=0: return x Else: return -X
If you have already saved the code for the My_abs () function as a funct1.py file, switch to the funct1.py storage directory with the CD C:\work after you open cmd and enter the python To start the Python interpreter and then enter the from funct1 import my_abs to import the My_abs () function, you can enter a cmd such as my_abs ( -97) to invoke the function and display the result
Note:
(1) When defining functions, you need to determine the number of function names and parameters
(2) When the function is finished and there is no return statement, auto return None
(3) A function can return multiple values at the same time, but is actually a tuple.
3. Parameters of the function
Python Learning 2 function (Liaoche)