Python Recursive Function
Python Recursive Function
if x == 1:
return 1
else:
return (x * factorial(x-1))
num = 3
print("The factorial of", num, "is", factorial(num))
Run Code
Output
The factorial of 3 is 6
Each function multiplies the number with the factorial of the number below
it until it is equal to one. This recursive call can be explained in the
following steps.
Every recursive function must have a base condition that stops the
recursion or else the function calls itself infinitely.
The Python interpreter limits the depths of recursion to help avoid infinite
recursions, resulting in stack overflows.
By default, the maximum depth of recursion is 1000 . If the limit is crossed, it
results in RecursionError . Let's look at one such condition.
def recursor():
recursor()
recursor()
Output
Traceback (most recent call last):
File "<string>", line 3, in <module>
File "<string>", line 2, in a
File "<string>", line 2, in a
File "<string>", line 2, in a
[Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded