程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python functions

編輯:Python

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 .

Defined function

Format :

def Function name ():
Code 

Empty function

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

Parameter check

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 .

Return multiple values

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 .

Call function

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 .

The parameters of the function

Positional arguments

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 .

Default parameters

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 .

Variable parameters

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 .

Named key parameters ( unfinished )

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 .

Recursive function

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 .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved