Callback function, recursive functions
Callback
Callback function called callback function, refers to the function passed as an argument to execute an additional function. A function for example, passed as a parameter to the function B. A function is then performed in the B function. The advantage of this approach is that you can use the function before the function is defined, or API call for other programs offered (available as function). More abstract concept, look at the following example:
def func(num,fun):
fun(num)
def f1(x):
print("这是f1函数",x)
def f2(x):
print("这是f2函数",x)
func(1,f1)
func("hello",f2)
result
这是f1函数 1
这是f2函数 hello
The figure is a function of three memory address, let's start running the first function call
First, the position parameter passing to another function variable, in which case num = 1 fun = f1 function memory address at memory address just defined function f1, will then run down to achieve print 这是f1函数 1
recursive function
Inside the function, you can call other functions. If a function calls itself within itself, this function is a recursive function, the following example is to call itself.
#例子1
def foo(n):
print(n)
n += 1
foo(n)
foo(1)
Run the following codes
#例子2
def func(num):
print(num)
if num > 0:
func(num -1)
else:
print('--------')
return num
res = func(3)
print(res)
Each function call will have a namespace of its own, if you have been to call it, would create a name space take up too much memory problems.
When the judge began to return this time out of what he would return it?
func (1-1) has been return what's next return?
Next will return func (2-1) return func (3-1) is the end result return func (3)
Believe that here a little Mongolian, think, most people do things, interrupting the first thing, the second thing to do is to arrange and they will follow the first thing to forget things, If the time to do the second thing, has been interrupted, is scheduled to do the third thing, will the first member, a second member of the follow-up thing to do to forget ...... this is recursive function not understand the reason.
Here again an example 3
def age(n):
if n == 1:
return 40
else:
return age(n-1)+2
print(age(4))
Detailed look at the process of running the above code
When calling the function itself, the code does not end after it, but in the waiting condition is False, then again after execution of the code, the same color print () function statement waits for the corresponding color.
Starting var:.. n = 4
23:14:57.157933 call 4 def age(n):
23:14:57.157933 line 5 if n == 1:
23:14:57.157933 line 8 return age(n-1)+2
Starting var:.. n = 3
23:14:57.157933 call 4 def age(n):
23:14:57.157933 line 5 if n == 1:
23:14:57.157933 line 8 return age(n-1)+2
Starting var:.. n = 2
23:14:57.157933 call 4 def age(n):
23:14:57.158934 line 5 if n == 1:
23:14:57.158934 line 8 return age(n-1)+2
Starting var:.. n = 1
23:14:57.158934 call 4 def age(n):
23:14:57.158934 line 5 if n == 1:
23:14:57.158934 line 6 return 40
23:14:57.158934 return 6 return 40
Return value:.. 40
23:14:57.158934 return 8 return age(n-1)+2
Return value:.. 42
23:14:57.158934 return 8 return age(n-1)+2
Return value:.. 44
23:14:57.158934 return 8 return age(n-1)+2
Return value:.. 46
See the above process, when n = 1, this time in memory 40 returns directly to age (1) arises. Here it will finish second to last thing that age (2-1) +2. Memory now has age (1) = 40, then age (1) + 2 = age (2) = 42, and age (2) is present in memory, so!
age(3-1)+2=age(3)=44
age(4-1)+2=age(4)=46
The return value is the final 46
Example 1 will appear after the error code is run
using a recursive function needs to be taken to avoid stack overflow. In the computer, the function call is achieved through the stack (Stack) This data structure, each time a function call into the stack a stack frame is increased by one, each time the function returns, the stack will be reduced one stack frame. As the stack size is not unlimited, so the number of recursive calls too, can cause a stack overflow. So python order to stop this phenomenon, forced the recursive layers of control in the 997, you can also change the default recursion depth, if the problem does not solve the recursive layer 997 are either not suitable for use recursion to solve.