Understanding the Recursion: Pros & Cons

Before going to the complex examples and facts about recursion, let’s begin with the basic to understanding the Recursion by knowing its definition and few conditions.

Recursion is the method which call itself again and again until it meets the base condition. Recursion has two conditions-

  1. Base Condition(breaking condition)
  2. Recursive Condition

 

For example,

Understand the Recursion: Pros

Factorial of a number is:

Understanding the Recursion with factorial logic

Now on the basis of recursive function,

  • Base condition:
if(n==0) 

{

  return 1;

 }

 

  • Recursive Condition:
If(n>0)
 {
 return  n * fact(n-1);

 }

 

Step-by-Step Explanation with the help of image :

 

Understanding the Recursion step by step

 

Now convert it in a program:

  1. Using iteration
include <stdio.h> int main() { 
int n, i;   
int factorial = 1;   
printf("Enter an integer: ");
scanf("%d",&n);     // show error if the user enters a negative integer 
if (n < 0)        
   printf("Error! Factorial of a negative number doesn't exist.");    
    else     {         
        for(i=1; i<=n; ++i)       
          {    factorial *= i;     // factorial = factorial*i;        
         }         printf("Factorial of %d = %d", n, factorial);    
          }     
    return 0;
 }

 

 

  1. Using Recursion
#include <stdio.h>

 int fact(int n);

int main()

{

    int n;

    printf("Enter a positive integer: ");

    scanf("%d", &n);

    printf("Factorial of %d = %ld", n, fact(n));

    return 0;

}

 int fact(int n)

{

    if (n >= 1)

        return n*fact(n-1);

    else

        return 1;

}

 

 

Above here you see the pros of the recursion, but when something has pros then it also has some cons. Let’s look its cons then,

Understand the Recursion: Cons

Sometimes when you need a output of a large value in a recursion, the compiler slows down but it will not be same in iteration. Here, I’m explaining it with the help of Fibonacci series.

 

understanding the Recursion by comparison with iteration

 

In this example, the code written in RED is iterative in nature and it will find out your long output just by adding the result the previous result but in the second, the Blue one it is recursive. It will call the function again and again until it meets the base condition and in the result the compiler takes much time as compared to the Iterative one.

 

The program of the above example is,

  1. Iterative form
# include <stdio.h>

void main()

{

 int a = -1, b = 1, c = 0, i, n, sum = 0 ;

 printf("Enter the limit : ") ;

 scanf("%d", &n) ;

 printf("\nThe fibonacci series is : \n\n") ;

 for(i = 1 ; i <= n ; i++)

 {

  c = a + b ;

  printf("%d \t", c) ;

  sum = sum + c ;

  a = b ;

  b = c ;

 }

 printf("\n\nThe sum of the fibonacci series is : %d", sum) ;

}

 

  1. Recursive form

 

#include <stdio.h>

int fibo(int);

int main()

{

    int num;

    int result;



    printf("Enter the nth number in fibonacci series: ");

    scanf("%d", &num);

    if (num < 0)

    {

        printf("Fibonacci of negative number is not possible.\n");

    }

    else

    {

        result = fibo(num);

        printf("The %d number in fibonacci series is %d\n", num, result);

    }

    return 0;

}

int fibo(int num)

{

    if (num == 0)

    {

        return 0;

    }

    else if (num == 1)

    {

        return 1;

    }

    else

    {

        return(fibo(num - 1) + fibo(num - 2));

    }

}

 

>> when you run the above program with a large value of n(say 40), the compiler will take time in recursive function whereas it will compile faster in iterative form.

Hence, the Recursive function is Time-taken and create overhead over the compiler.

Note : This post is to Understand the ‘pros & cons’ of recursion with the help of example.For any query or any request for a particular topic you can comment in comment box.

A Salesforce Developer at AlmaMate Info Tech PVT LTD. An Aligarian and also your query solver in database field.This site mark the difference as Black or White to make you choose the best for you. Don't feel pressurized, feel confident..!!

Leave a reply:

Your email address will not be published.