Featured Post

advertisement

Recursion in python

 

Python Recursion

Python Recursion

Python also accepts function recursion, which means a defined function can call itself.

Recursion is a common mathematical and programming concept. It means that a function calls itself. This has the benefit of meaning that you can loop through data to reach a result.

Following is an example of a recursive function to find the factorial of an integer.

Factorial of a number is the product of all the integers from 1 to that number. For example, the factorial of 6 (denoted as 6!) is 1*2*3*4*5*6 = 720.

1
2
3
4
5
6
7
8
9
10
11
12
def factorial(x):
    """This is a recursive function
    to find the factorial of an integer"""
 
    if x == 1:
        return 1
    else:
        return (x * factorial(x-1))
 
 
num = 3
print("The factorial of", num, "is", factorial(num))

Comments

Advertisement

Popular posts from this blog

ADVERTISEMENT