I. Why use a function:
Resolving code reuse issues
Unified Maintenance
The organization structure of the program is clear and readable
Two. Defining functions
First define after use!!
def funcname (Arg1,arg2,...):
"" "Description Information" ""
function body
return value
Defining functions can be divided into three categories:
No parameter function
Foo ()
No return value required
Parameter function
Bar (x, y)
Null function
function Body Write Pass
Three. Calling functions
Statement form: foo ()
Expression form: res=10*my_sum
Function call as a parameter of another function: print (my_sum)
Four. return value of function
can return any type
Returns none when no return
Return returns a value
Multiple values can be returned, expressed in tuples
Only one return can be performed
Five. Parameters of the function
Arguments and formal parameters
def foo (x, y)
Print (x)
Print (y)
At the angle of the argument
1. Transfer Values by location
Foo (ON)
2. Pass the value by keyword
Foo (x=1,y=2)
3. mixed with
foo (1,y=2)
The value passed by location must be preceded by the keyword
foo (y=2,1) Error
Span style= "font-family:"microsoft yahei"; Font-size:16px "> can only be assigned one time for a parameter
***** analysis of
positional parameters: parameters that must be passed the value
Default parameters: No need to pass value (can not be transmitted)
Default parameters must be placed after the position parameter
six. *args and **kwargs
*args
* Give him the number after the location value (tuple)
def foo (X,*args):
Print (x)
Print (args)
Foo (1,2,3,4,5)
*args mixing with positional parameters and default parameters
**kwargs
* * Give him the number after the keyword is passed (dictionary)
def foo (X,**kwargs):
Print (x)
Print (Kwargs)
Foo (1,y=1,z=2)
Mix it in.
def foo (X,*args,**kwargs):
Print (x)
Print (args)
Print (Kwargs)
From the angle of the formal parameter
def foo (**kwargs):
Print (Kwargs)
Foo (x=1,y=2,z=3)
Angle of argument
def foo (x,y,z=1):
Print (x)
Print (y)
print (z)
python\ function