A function is like a box that packages some related functions into a function for calling. Within a function, other functions can be called, or the function itself can be called.
If a function calls itself internally, then this is a recursive function.
Let’s take a simple example.
In mathematics we know the factorial of 100, 100!=1009998...321.
Breaking it down, 100!=10099!,99!=9998!,98!=98*97!...
Then we define A function to calculate factorial:
def fact(n): return n*fact(n-1)
The function above looks like a number that is constantly multiplied by a number smaller than 1 without stopping. Therefore, we must modify the above function so that it can stop after calculation when n=1.
def fact(n): if n==0: return 1 else: return n*fact(n-1)
Recursive functions can also be implemented in a loop. But the recursive function logic is relatively simple. However, when using recursive functions, care should be taken to prevent stack overflow caused by too many recursive function calls.