What is Recursion in C?

Welcome guys, in this lecture we are going to discuss What is recursion in C, its importance, how to use it, where and when to use it, its advantages and disadvantages, etc, till now we have talked a lot about C Programming, and forgoing further in this module, I recommend you to refer our previous module titled what is a function in c, to get this concept clearer.

Let’s dive into this lecture to get the concept clearer.

What is Recursion in C?

Suppose you are working with a function, and if you have to use or call that function in the same function only, then this process is called recursion, i.e., function calling itself. In Programming, if you want to repeat that function in a similar way inside that function only, then you can achieve this by` using the technique of recursion.

What is Recursion in C

If a program, allows you to call the function inside the same function, then this call is called a recursive call. C Programming supports this wonderful technique of recursion, function to call itself. But we have to be very concentrated while working with recursion because if the exit condition is not met then the program can move into an infinite loop that can be devasted.

In simple words, the recursive function is the function which calls itself in their scope, and this special technique is called recursion.

The following syntax clearly represents the use of recursion.

void recurse()
{  
   ………….
   recurse();
   ………….
}
int main()
{
   ………..
   recurse();
   ………..
   return 0;
}

In the above syntax, firstly we have created the function recurse, and then we have called the same function recurse inside that function, so this is what is known as a recursive call. It will keep continuing until some conditions are met. There must be some exit condition by using some conditional statement such as if-else so that one branch can make a recursive call and the other doesn’t, so it helps in preventing the infinite recursion, and then lastly the function has been called inside the main function.

The diagrammatic representation is as follows.

Diagrammatic Representation

These functions are used to solve many useful problems, whether it be complex mathematical problems or can be anything, it can be used to solve problems such as listing a series of Fibonacci and in the same manner to compute the factorial of a given number.

Let’s see an example.

Write a program that calculates the factorial of an entered number

#include <stdio.h>
int factorial (int n)  {
   if (n > 0)       // exiting condition for the recursive call
       {       
   return n * factorial (n-1) ;           //recursive call 
       }
else {
return 1 ;
         }
}
int main( )
{
   int n;
   int result =0;
   printf(“Enter the Number: \n”);
   scanf(“%d”, &n);
   result = factorial(n);         // function called
   printf(“The result is: %d”, result);
   return 0;
} 

The output of the above program is:

Enter the Number: 5
The result is: 120

In the above program, we are calculating the factorial of the given number by using the recursion technique, in a function named factorial we have given the condition like the number should be greater than 0, so until or unless this condition fails the program will keep executing and as he conditions failed, then it will stop execution and comes out of the function. After this process the main function executes and we got the desired result.

What are the advantages of recursion?

Since we have learned a lot about recursion, Let’s focus on some of the advantages of using recursion.

  • It makes the code simpler and shorter.
  • You only need to define two cases and which are base case and recursive case, this makes the program more efficient.
  • There are many problems that are recursive by their nature and that are Tree traversal and Graphs.

What are the disadvantages of using recursion?

Working with recursion is not an easy task, It has the following disadvantages also.

  • It requires more space complexity, that it consumes more memory, as the program is executed, it will occupy the stack until the base case is reached.
  • It requires more time complexity i.e., consumes more time for execution because as each time the function called the stack grows, and the final answer is only returned when the content of stacks is popped completely.

So, we have completed this module, hope you all liked and enjoyed this module, it’s a little complex but very interesting, you all must be excited about the upcoming modules.

Until then, stay connected, happy coding, keep practicing, and learning.