0% found this document useful (0 votes)
87 views

Python Recursive Function

A recursive function in Python is a function that calls itself during its execution. An example is a recursive function to calculate the factorial of a number, which calls itself recursively by decreasing the number until it reaches the base case of 1. Each call multiplies the number by the factorial of the lower number until the base case is reached. Recursive functions must have a base case condition to stop the infinite recursion, otherwise it will result in a RecursionError due to exceeding the maximum recursion depth of 1000.

Uploaded by

Piyush Manchanda
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

Python Recursive Function

A recursive function in Python is a function that calls itself during its execution. An example is a recursive function to calculate the factorial of a number, which calls itself recursively by decreasing the number until it reaches the base case of 1. Each call multiplies the number by the factorial of the lower number until the base case is reached. Recursive functions must have a base case condition to stop the infinite recursion, otherwise it will result in a RecursionError due to exceeding the maximum recursion depth of 1000.

Uploaded by

Piyush Manchanda
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Python Recursive Function

In Python, we know that a function can call other functions. It is even


possible for the function to call itself. These types of construct are termed
as recursive functions.
The following image shows the working of a recursive function
called  recurse .

Recursive Function in Python


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 .

Example of a recursive function


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

Output

The factorial of 3 is 6

In the above example,  factorial()  is a recursive function as it calls itself.


When we call this function with a positive integer, it will recursively call itself
by decreasing the number.

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.

factorial(3) # 1st call with 3

3 * factorial(2) # 2nd call with 2

3 * 2 * factorial(1) # 3rd call with 1

3 * 2 * 1 # return from 3rd call as number=1

3 * 2 # return from 2nd call

6 # return from 1st call

Let's look at an image that shows a step-by-step process of what is going


on:
Working of a recursive
factorial function
Our recursion ends when the number reduces to 1. This is called the base
condition.

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

You might also like