Python Recursion

By | April 18, 2019

In this tutorial, we will discuss what are recursive functions and how to create them in python and why we need them.

Table of content

  • What are recursive functions
  • Python recursive functions
  • Some pro and cons of Recursion function

What are recursive functions

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

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

We have said that recursion act like loops what can perform repetitive work so there should be a base condition that stops that repetitive statement and give a result or else you will get trap in the infinite loop.

Python recursive Function

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

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

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

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

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

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 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 does it work.

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

that how at last it returns a value 7*6*5*4*3*2*1 and we get an output 5040. Here you can see that the base condition play 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 recursion function, the code looks neat
  2. It divides the functionality into smaller parts which make the programme easy to understand
  3. It comes very usefully with the binary trees data structure

Disadvantages:

  1. It’s very difficult to understand the logic behind every recursive function.
  2. A recursive function is slow as compare 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

 

Category: Python Tags:

About 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. Required fields are marked *