Python Recursion Function
If we call a function from within the same function, then it is called Recursion Function.
Finding factorial of a given number is a classic example for Recursion Function.
In the following program, we read a number from user, and find its factorial using the recursion function.
factorial() is the recursion function. Please observe that inside factorial() function, we are calling the same factorial() function, but of course with modified argument.
def factorial(n): result = 1 if n > 0: if n > 1: return n * factorial(n - 1) return result n = int(input('n : ')) print('n! =', factorial(n))
n : 5 n! = 120
n : 12 n! = 479001600
In this tutorial, we learned what a Recursion Function is, and how does that work in a program, with examples.