Tag: is the tail recursive efficiency stack Overflow save optimization return problem condition
Recursion is the function that calls this function internally.
Characteristics of recursion:
1. There must be a definite end condition, otherwise it will turn into a dead loop, the final detonation system.
2. Each time you enter a deeper level of recursion, the problem size should be reduced compared to the previous recursion.
3. Recursive execution is inefficient, and too many recursive hierarchies can cause stack overflow.
Example: Recursion
def factorial (n):
If n==1:
Return 1
Return N*factorial (n-1)
Print (factorial (4)) #输出4的阶乘
Tail-Recursive optimization:
def CAL (N):
Print (n)
Return Cal (N+1)
Cal ()
The variables and contents of the functions executed at each recursive time are not saved in the stack, so the recursive stack is not full.
Recursion of Python