Function
- A group of statements that completes a particular function, which can be used as a unit, and give it a name of a group statement, that is, the name of the functions
- The function name can be executed more than once in different places of the program, i.e. function calls
- Predefined functions (can be used directly)
- Custom Functions (self-written)
Definition of the function:
- def function sentence ([parameter list])://Definition
```
def hello ():
Print (Hello World)
#### 函数的调用:- 函数名([参数列表]) //调用
Hello ()
#### 函数的参数:形式参数和实际参数- 在定义函数时,函数名后面的括号中的变量名称叫做 形式参数, 或者称为 形参- 在调用函数时,函数名后面括号中的变量名称叫做 实际参数,或都称为 实参
A = ' Hello '
b = ' World '
def hello (x, y)://x y as formal parameter
Print (' {0} {1} '. Format (x, y))
Hello (A, B)//A, b this is the actual parameter
#### 函数的默认参数:- 在定函数时,也可设置形参的缺省值 (缺省参数)
def func4 (x, y=100):
Print (' x = {0} '. Format (x))
Print (' Y = {0} '. Format (y))
Func4 (10, 50)
FUNC4 (10)
"""
x = 10
y = 50
x = 10
y = 100
It can be seen from the results. When a second argument is passed to the call, the default value takes effect when the pass-through argument is not passed in
"""
- 函数的多类型传值和冗余参数
# *var_args_tuple and **var_args_dict (variable length parameter) # Use *var_args_tuple and **var_args_dict# *var_args_tuple when a function needs to process more arguments than is declared Passed in is a tuple type def func5 (x, Y, *z): print (' x = {0} '. Format (x)) print (' Y = {0} '. Format (y)) print (' z = {0} '. Format (z ) print (' Lenth of Z is: {0} '. Format (len (z)))] for I in Z:print (i) Func5 (Ten,, Max, Max) #等价于 # Tuple_te St = ((+, +, +) # Func5 (Ten, Tuple_test) "" "" "x = 10y = 20z = (+, +, x) Lenth of z is:430405060 from the results can be seen, x, Y, respectively Corresponds to the first two arguments 10 20, the remaining parameters 30 40 50 60 as a tuple passed to the parameter Z "" "# **var_args_dict passed in is a dict type Def func6 (A, B, **c): print (' A = {0} '. Mat (a)) print (' B = {0} '. Format (b)) print (' c = {0} '. Format (c)) print (type (c)) for key, Valaue in C.iteritems (): Print (' key = {0}, Valaue = {1} '. Format (key, Valaue)) Func6 (x, x= ' Hello ', y= ' World ') # equivalent to # dict_test = {' x ': ' He Llo ', ' Y ': ' World '}# Func6 (Ten, **dict_test) "" "a = 10b = 50c = {' Y ': ' World ', ' x ': ' Hello '}<type ' dict ' >key = y, V Alaue = Worldkey = x, ValaUE = Hello from the results can be seen, 10 passed to the formal parameter a, 50 to the formal parameter B, x= ' Hello ', y= ' world ' as a dictionary passed to the formal parameter C ""
#### 函数的变量##### 局部变量和全局变量- python 中的任何变量都有特定的作用域- 在函数中定义的变量一般只能在该函数内部使用,这些只能在程序特定部分使用的变量我们称之为局部变量- 在一个文件顶部定义的变量可以供文件中的任何函数调用,这些可以为整个程序所使用的变量称为全局变量
x = 100//global variable
def func ():
x = 200//local variable
Print (x)
Func ()
Print (x)
200 Calling x = 200 inside the function
100 Calling X = 100 outside the function
##### 函数中操作局部变量- 在函数中要以调用全局变量
x = 100
def fun ():
Print (x)
Fun ()
100//In the function to invoke the global variable
- 正常情况下了函数中不能操作全局变量
x = 100
def fun ():
x = x + 1
Print (x)
Fun ()
unboundlocalerror:local variable ' x ' referenced before assignment//error
- global 声明一个全局变量- 使用global 在函数中操作全局变量
x = 100
def fun ():
Global X
x = x + 1
Print (x)
Fun ()
Print (x)
101//global variable x performs a +1 operation in the function and prints
101//global variable changed
- 使用global 在函数中声明全局变量
x = 100
def fun ():
Global X
x = x + 1
Print (x)
Global y
y = x + 100
Fun ()
Print (x)
Print (y)
101
101
201
#### 函数的返回值 - 函数被调用后会返回一个指定的值- 函数调用后默认返回None- return 返回值- 返回值可以是任意类型- return 执行后,函数终止- return 和 print的区别 print中是将执行结果打印出来,无法对处理结果进行再次使用 return 可以将执行结果进行判断或赋值给变量,可以对结果进行使用#### #### 函数的递归- 在函数中调用本身例:计算阶乘正常方法
def factorial (n):
sum = 1
For n in xrange (1, n+1):
Sum *= N
return sum
Print (factorial (5))
使用递归
def factorial (n):
if n = = 0:
Return 1
Else
return n * Factorial (n-1)
Print (factorial (5))
```
Considerations for recursion
- Must have the last default result (if n = = 0)
- Recursive parameters must converge to the default result (n-1)
Python review 4-1 functions, parameters, return values, recursion