What is a function
Definition: A function is a collection of a set of statements by a name (function name) encapsulated, in order to execute this function, only need its function name call can
Characteristics:
1. Reduce duplication of code
2. Make the program extensible
3. Make the program easier to maintain
Syntax definitions
def Weekday (): Print ("hello") weekday ()
can take parameters
def Calt (x, y ): = x * y return##返回函数执行结果 (any value can be, because it only needs to return a result, and then take it to do something else)CALTT = Calt (10,20)print(CALTT)
return value
To get the result of the function execution, you can return the result using the return statement
Attention:
1. The function will stop executing and return the result as soon as it encounters a return statement, so it can also be understood that the return statement represents the end of the function.
2. If return is not specified in the function, the return value of this function is None
##嵌套函数Name ="Caicai"defname_list (): Name="Caicai" defname_list2 (): Name="Caicai" Print("This is the SAN layer print:", name) name_list2 ()Print("This is the second layer of printing:", name) name_list ()Print("the outermost:"Name
# # #递归 def Calt (n): Print (n) if Int (N/2) >0: return calt (int (N/2)) Calt (+) " characteristics of recursion: 1. There must be a clear end condition 2. Each time a deeper level of recursion is reached, the problem size should be reduced by 3 compared to the last recursion . The recursive efficiency is not high, the recursive hierarchy too much will cause the stack overflow (in the computer, the function call is implemented through the data structure of the stack), Whenever a function call is entered, the stack adds a layer of frames, and whenever the function returns, the stack is reduced by a layer of frames. Because the size of the stack is not wireless, too many recursive calls can cause stack overflow)'
Python function 01