Python recursive function

What is python recursive function?

Recursion is a way of programming in which a function calls itself for one or more times. A Python function that 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. The recursive function body also contains a base condition to prevent the function from 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 the 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

Syntax of recursive function in python:

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

In the 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 as well. This base condition gets evaluated after each iteration of the 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. Due to this, the recursion will never end and it will go into an 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 python 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 the 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:

  • The 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 recursive function:

  • Though the use of recursive function makes the code look clean however it is hard to follow and understand.
  • It is very tough to debug a complex recursive function.
  • The memory consumption of the recursive function is very high.

Continue Reading:

Leave a Comment