What is a recursive function?
Inside the function, you can also continue to call other functions, if a function calls itself internally, this function is a recursive function
Give an example of a factorial that asks N:
def fact (n): if n = = 1:return 1; Else : return N * Fact (n-1)Print fact (5)>>>120
The above is a recursive function
The procedure for running this function is as follows:
===> fact (5)===> 5 * FACT (4)===> 5 * (4 * FACT (3))===> 5 * (4 * (3 * FACT (2))) /c4>===> 5 * (4 * (3 * (2 * FACT (1))) ===> 5 * (4 * (3 * (2 * 1)))===> 5 * (4 * (3 * 2)) ===> 5 * (4 * 6)===> 5 * 24===> 120
Note: However, the use of recursive functions need to prevent stack overflow, function calls through the stack (stack) This data structure to achieve, whenever entering a function call, the stack will add a stack of frames, whenever the function returns, will reduce the stack frame, because the size of the stack is not infinite, so, Excessive number of recursive calls can cause stack overflow
2017 Winter Vacation 0 The recursive function of the function of the basic Learning Python series