Python Recursion Function

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.

Python Program

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))

Output #1

n : 5
n! = 120

Output #2

n : 12
n! = 479001600


In this tutorial, we learned what a Recursion Function is, and how does that work in a program, with examples.