What is recursive function in python?

Recursion is a way of programming in which a function calls itself for one or more times. A Python function which is defined to call itself during its execution is known as python recursive function. This way of programming enables the function to be executed several times. Recursive function body also contains a base condition to prevent the function getting into an infinite loop. So that was the definition of python recursive function, Now let’s proceed to understand each term in detail.

In last few articles, We have learned about how to define and call a python function. Also we have learned that, A function can call another function, likewise it is also possible to call a function itself (Recursion).

Recursive function is python

Python recursive function syntax:

def sample_function(arguments):     '''This is a recursive function.'''    <Base condition>
sample_function (arguments)

In above syntax, We are defining a new recursive function named “sample_function” and after the “return” statement we are calling the function (sample_function) itself within its own body. So every time the function “sample_function” gets executed after the “return” statement it will again call itself.

While defining a recursive function, We must define a base condition defined as well. This base condition gets evaluated after each iteration of function call and the time when this base condition evaluates to ‘True’ the recursion ends.

Therefore if we don’t have the base condition defined in a recursive function, The function will keep on calling itself and the recursion will never end and it will go into infinite loop.

Examples of python recursive function:

Example 1: Python program to calculate factorial number using recursive function.

Let’s first see how factorial number is calculated. Suppose we want to calculate the factorial number of 4,

4! = 4*3*2*1 = 24. Here 24 is the factorial number of 4!.

#Python program for factorial number calculation using recursive function.
def factorial_calculator(fact_number):
'''This is python recursive function to
 calculate the factorial number'''

#Base condition
if fact_number == 1:
return fact_number * factorial_calculator(fact_number - 1) 


Please enter a number for factorial calculation:4

In above example, We have defined a python recursive function named “factorial_calculator” to calculate the factorial number. This “factorial_calculator” function is a recursive function that calls itself.

When we call this function (e.g print(factorial_calculator(4)) ) with a number ‘4’ as an argument this function starts calling itself. However after each iteration argument ‘4’ is decremented by 1.

Here we have defined the base condition(if statement) which checks whether the argument value is 1. The time when the argument value becomes 1 the recursion ends.

Example 2: Python program to do the sum of first n natural numbers.

Suppose we want to calculate sum of natural numbers till number 5.

sum_of_numbers = 5+4+3+2+1

#Python program to calculate sum of n natural numbers.
def sum_of_numbers(number):
    '''This is python recursive function to
    calculate sum of n natural numbers'''
    if number == 1:
        return 1
    return number + sum_of_numbers(number - 1)



Advantages of python recursive function:

  • Use of python recursive function makes your code look neat and clean.
  • Generating sequence is easy by using recursive function than using any other nested loop.
  • Recursion makes it easy to code, Since we can break the code into smaller tasks.

Disadvantages of python recursive function:

  • Though use of recursive function makes the code look clean but its hard to follow and understand.
  • It is very tough to debug a complex recursive function.
  • Memory consumption of the recursive function is very high.

Continue Reading:

Share this Post

Leave a Comment

Your email address will not be published.