Recursion in C language

“The expert in anything was once a beginner”

Hello everyone!!!!
Today we are going to have a look on Recursive function in C , its definition, how it exactly works, advantages and disadvantages.
Soo let’s get started 🙂

Definition :-
The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function. Using recursive algorithm, certain problems can be solved quite easily.

Explanation:-
1)Recursion is the process which comes into existence when a function calls a copy of itself to work on a smaller problem.
2)Any function which calls itself is called recursive function, and such function calls are called recursive calls. Recursion involves several numbers of recursive calls. However, it is important to impose a termination condition of recursion.
3)Recursion code is shorter than iterative code however it is difficult to understand.

•Syntax:-
void recurse()
{
….. … … … … .
recurse()
… … … .. … ..
}
int main()
{
… … … … .. …
recurse()
… … … … . …
}

Syntax of recursive function

This is all about explanation part, now let us try to understand with the help of some examples below.

Q1) Write a recursive function in ‘C’ to calculate factorial of a number. Use this function in main.

#include<stdio.h>
int fact (int); 
int main() 
{ 
  int n,f; 
  printf("Enter the number whose factorial you want to calculate:"); 
  scanf("%d",&amp;n); 
  f = fact(n); 
  printf("factorial = %d",f); 
}

int fact(int n) 
{ 
  if (n==0) 
  { 
    return 0; 
  } 
  else if ( n == 1) 
  { 
    return 1; 
  }
  else 
  { 
    return n*fact(n-1); 
  } 
}

Output:-
Enter the number whose factorial you want to calculate:5
factorial = 120

Q2) Write a recursive ‘C’ function to calculate sum of digits of a number.

#include<stdio.h>

int sum_of_digit(int num)
{
  if (num== 0) return 0;
  else return ((num % 10) + sum_of_digit(num / 10));
}

int main ()
{
 int num,result;
 printf("Enter number");
 scanf("%d",&amp;num);
 result = sum_of_digit(num);
 printf("Sum of digit is %d",result);
 return 0;
}

Output:-
Enter number123
Sum of digit is 6

Advantages of recursion:-
1) It can reduce time complexity and has a relaxation on the number of iterations( we can run a variable number of loops ). It is easy to implement.
2)Recursion adds clarity and (sometimes) reduces the time needed to write and debug code (but doesn’t necessarily reduce space requirements or speed of execution).
3)Performs better in solving problems based on tree structures.

Disadvantages of recursion:-
1)It can throw a StackOverflowException since it consumes a lot of memory.
2) Recursive function logic sometimes difficult to construct.
3) If proper coding is not done, then the recursive function may lead to infinite loop. During recursive function calls, recursion requires more time and memory because of indirect computation of time and memory.

Soo this is all about Recursion!
I hope you’ll must got an idea how exactly it works.

Soo see you all in the next article… . Till then good bye 🙂
Have a wonderful day ahead 🙂



You may also like...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: