Python Recursion

By | April 14, 2022
Python Recursion

This tutorial will discuss what recursive functions are and how to create and why we need them in python. So let’s discuss Python Recursion.

Python Recursion

What are recursive functions?

Until now, we were defining a function, and we were calling it from outside the function at the time of execution. What if we call the function inside the function? It will create a kind of loop of what recursion functions are all about.

A recursive function is a function that can call itself during execution.  For example, when a function body has a statement in which it calls itself, that function will be termed a recursive function.

We have said that recursion acts like loops that can perform repetitive work, so there should be a base condition that stops that repetitive statement and give a result. You will get trapped in the infinite loop.

Python Recursive Function

There are many examples in which a function calls another function, but those are not recursive functions only those functions are recursive functions that can call themselves.

Finding the factorial of a number is one of the classic examples of recursive functions.

If you do not know what a factorial is, let’s understand it first. Factorial is a Math concept denoted by an exclamation mark (!). If you want the factorial of 5, you will write 5! And the solution of the factorial would be

Vamware

5!= 5 x 4 x 3 x 2 x 1 = 120

And for note 1! =1 and 0! =1

Vamware

Let’s code the factorial in python

def fact(n):
    if n==1 or n==0:                           #Base condition
        return 1
    else:
        return n * fact(n-1)                    # recursion function calling

num=7
print("the factorial of" ,num, "is ", fact(num))                     #function calling

 

#Output

the factorial of 7 is 5040

Behind the code:

In the above example, to execute the function fact(), we call it from outside the function, then the value of variable num gets assigned to the function argument n, and the function gets started to execute when the argument n does not satisfy the if statement the else statement gets executed. In the else statement, we strike the recursive function, and let’s see how it works.

Vamware
fact(n)                  #main function
    return 7 * fact(6)                #recursive function first call
        return  7 * 6 *fact(5)            #recursive function 2nd call (the value of fact(6) is 6 * fact(5))
            return 7*6*5*fact(4)              #recursive function 3rd call
                return  7*6*5*4 *fact(3)           #recursive function 4th call
                    return 7*6*5*4*3*fact(2)            #recursive function 5th call
                        return 7*6*5*4*3*2*fact(1)          #recursive function 6th call
                            return 7*6*5*4*3*2*1                  #recursive function 7th call

At last, it returns a value of 7*6*5*4*3*2*1, and we get an output of 5040. Here you can see that the base condition plays a significant role what if we did not pass the base condition, the number kept on decreasing, and it could make an infinite recursive call.

Pro and Cons of recursive function:

Advantages:

  1. With the recursion function, the code looks neat
  2. It divides the functionality into smaller parts which make the program easy to understand
  3. It comes very usefully with the binary trees data structure

Disadvantages:

  1. It isn’t easy to understand the logic behind every recursive function.
  2. A recursive function is slow as compared to the iterators.
  3. It occupies a lot of memory because we are executing the same function again and again
  4. it is very hard to debug the recursive function.
  5. With a big set of data, recursive functions are not useful

Python Tutorials:

Introduction

Flow Control

Functions

Datatypes

File Handling

Object & Class

Additional Tutorials

Author: Sujata Gaur

Sujata Gaur is an engineering graduate who is a content writer by profession. Writing and learning are the two thing which is her passion. Looking around new things and writing about it gives another level of peace, this is what she presumes. Giving words to your thoughts is creativity and so she is creative. She has an interest in researching new ideologies and creating its content so as to make it easy and understanding.

Leave a Reply

Your email address will not be published.