Catalog
Defined function
Empty function
Parameter check
Return multiple values
Call function
The parameters of the function ( unfinished )
Positional arguments
Default parameters
Variable parameters
Named key parameters ( unfinished )
Recursive function
Functions are organized , Reusable , To achieve oneness , Or code snippets associated with functions .
Function can improve the modularity of application , And code reuse .
Format :
def Function name ():
Code
If you want to define an empty function that does nothing , It can be used pass
sentence (pass
Can be used as a placeholder ):
def nop():
pass
When you call a function , If the number of parameters is wrong ,Python The interpreter will automatically check out , And throw TypeError.
But if the parameter type is wrong ,Python The interpreter can't help us check .
The return value is one tuple! however , In grammar , Return to one tuple Brackets can be omitted , Multiple variables can receive one at the same time tuple, Assign corresponding value by position , therefore ,Python The function of returns multiple values is actually to return a tuple, But it's easier to write .
Format :
Function name ()
Every time a function is called , Functions are executed from scratch , When the code in this function is executed , It means that the call is over . Of course, if the function executes return It will also end the function .
example :
power(x) #x This is the position parameter
When there are more than two parameters in brackets , Arguments should be placed in the order of formal parameters , Pass in .
The default parameter can simplify the function call . The biggest advantage is that it can reduce the difficulty of calling functions .
def enroll(name,city='Beijing'):
print('name:',name)
print('city:',city)
enroll('Jim')
Be careful :
First, the required parameters are in front of , The default parameter is after , otherwise Python The interpreter of will report an error ( Think about why the default parameter cannot be placed before the required parameter );
The second is how to set the default parameters .
When a function has more than one parameter , Put the variable parameters ahead , Change small parameters behind . The parameter with small change can be used as the default parameter .
stay Python Function , Variable parameters can also be defined . seeing the name of a thing one thinks of its function , Variable parameter is that the number of parameters passed in is variable , It can be 1 individual 、2 From one to any one , It can also be 0 individual .
To define this function , We have to determine the input parameters . Because the number of parameters is uncertain , We first thought that we could a,b,c…… As a list or tuple Come in . But when it's called , You need to assemble one first list or tuple.
Define variable parameters and define a list or tuple Parameter comparison , Just add one before the parameter *
Number . Inside the function , Parameters numbers
What was received was a tuple, therefore , The function code is completely unchanged . however , When the function is called , You can pass in any parameter , Include 0 Parameters .
*nums
Express the nums
This list All elements of are passed in as variable parameters .
For keyword parameters , The caller of the function can pass in any unrestricted keyword parameter . As for what came in , You need to pass... Inside the function kw
Check .
Inside the function , You can call other functions . If a function calls itself internally , This function is a recursive function .
The advantage of recursive functions is that they are easy to define , Clear logic . Theoretically , All recursive functions can be written in a circular way , But the logic of the loop is not as clear as recursion .
Use recursive functions to prevent stack overflow . The way to solve the stack overflow of recursive calls is through Tail recursion Optimize , In fact, tail recursion has the same effect as loop , therefore , It is also possible to treat a loop as a special tail recursive function .
Tail recursion means , When the function returns , Call itself , also ,return Statement cannot contain expression . such , Compiler or interpreter can optimize tail recursion , Let recursion itself be called no matter how many times , It only takes up one stack frame , There will be no stack overflow .