A. Ask a question, why do you have a function?
1. Make the program simple
2. Improve the readability of your code
Definition of the function:
def function name (parameter 1, parameter 2): Comment of---function--- print (' function body ') return ' value '
Call to function:
return value = function name (parameter 1, parameter 2)
Parameters of the function: arguments and formal arguments. (Class two)
Argument: The value of the parameter passed in when the function is called.
Formal parameter: The name of the parameter defined when the function is defined.
Parameters: Divided into three categories.
1. The parameter can be any data type. 2. You can pass 0 or more parameters.
Position parameters:
Defining parameters sequentially
Default parameters:
First, do not set the default parameter for a mutable type, and you can set a default parameter for a parameter that has little variation.
If passed, overrides the default value of the default value is set before the function definition is OK.
Dynamic Parameters:
*args **kwargs
Arguments to invoke a function: pass the parameter by location:
Def My_max (A, b): print (A, b) My_max (10,20)
Follow the key word parameters:
def My_max ( A, A, b): Print (A, A, My_max (a), a=20)
The return value of the function:
Keyword for return value: return
The function of return:
1. Ending the execution of a function
2. Return the value you want to return
Two cases of return value:
One. The return value is None
There are three cases where the return value of the function is none.
1. Do not write the return value
2. Write only a return
3.return None (almost unused)
1. Do not write the return valuereturndeffunc1 (): a= 111b= [A]ret=func1 ()Print(ret)#2.returndefFunc2 (): a= 111b= [A] returnret=Func2 ()Print(ret)#3. Returndeffunc3 (): a=111b= [A] returnNoneret=func3 ()Print(ret) The return value of these three cases is none.
View Code
Two. The return value is not none
1. Returns a value
Receive with a variable
2. Return multiple values
Received with a variable, the result is a tuple
There are multiple return values that are received in the number of variables.
Python = = "function