Before we introduce recursive functions, we first introduce the following characteristics of the use of the following recursive functions:
1. A recursive function is a function that calls itself inside a function body
2. The recursive function of the operation of the scale to be reduced, so that can be calculated
3. The number of recursive layers do not exceed 999, because the function call is implemented through a stack (stack) of this data structure, whenever entering a function call, the stack will add a stack of frames, whenever the function returns, the stack will be reduced by a stack of frames. Too many calls to the layer will cause the stack to overflow.
4. Theoretically all recursive functions can be represented by loops, but recursion is more visually understandable (although I'm more accustomed to using loops ~ ~ ~ ~ ~ ~)
def factorial (n): ... if n ==1: ... return 1 ... Else :... return n*factorial (n-1) ... >>> factorial (5)120
The above code we use the loop to achieve ~~~~~
def factorial (n): ... FAC=1 ... while n>1: ... FAC=fac*n ... n-=1 ... return FAC>>> factorial (5)120
Python note seven (recursive function)