RECURSION
What is Recursion in C?
Recursion is the process of a function calling itself repeatedly till the given
condition is satisfied. A function that calls itself directly or indirectly is called a
recursive function and such kind of function calls are called recursive calls.
In C, recursion is used to solve complex problems by breaking them down into
simpler sub-problems. We can solve large numbers of problems using recursion in
C. For example, factorial of a number, generating Fibonacci series, generating
subsets, etc.
Recursive Functions in C
In C, a function that calls itself is called Recursive Function. The recursive functions
contain a call to themselves somewhere in the function body. Moreover, such
functions can contain multiple recursive calls.
Basic Structure of Recursive Functions
The basic syntax structure of the recursive functions is:
type function_name (args) {
// function statements
// base condition
// recursion case (recursive call)
}
Example: C Program to Implement Recursion
In the below C program, recursion is used to calculate the sum of the first N natural numbers.
// C Program to calculate the sum of first N natural numbers
// using recursion
#include <stdio.h>
int nSum(int n)
{
// base condition to terminate the recursion when N = 0
if (n == 0) {
return 0;
}
// recursive case / recursive call
}
int res = n + nSum(n - 1);
return res;
}
int main()
{
int n = 5;
// calling the function
int sum = nSum(n);
printf("Sum of First %d Natural Numbers: %d", n, sum);
return 0;
Output
Sum of First 5 Natural Numbers: 15
Fundamentals of C Recursion
The fundamental of recursion consists of two objects which are essential for any
recursive function. These are:
Recursion Case
Base Condition
1. Recursion Case
The recursion case refers to the recursive call present in the recursive function. It
decides what type of recursion will occur and how the problem will be divided into
smaller subproblems.
The recursion case defined in the nSum() function of the above example is:
int res = n + nSum(n - 1);
The recursion case if often represented mathematically is a recurrence relation. For the
above case:
f(N) = N + f(N - 1);
This recurrence relation is used for the complexity analysis of the method.
2. Base Condition
The base condition specifies when the recursion is going to terminate. It is the condition
that determines the exit point of the recursion.
Note: It is important to define the base condition before the recursive case otherwise, the
base condition may never encountered and recursion might continue till infinity.
Considering the above example again, the base condition defined for the nSum() function:
if (n == 0) {
return 0;
}
Now that the basic terminologies and fundamentals are out of the way, let’s move on to
understand how the recursion works in C.
Types of C Recursion
In C, recursion can be classified into different types based on what kind of recursive
case is present. These types are:
• Direct Recursion
-Head Recursion
-Tail Recursion
-Tree Recursion
• Indirect Recursion
recursion, syntax,  types, example program
recursion, syntax,  types, example program
recursion, syntax,  types, example program
recursion, syntax,  types, example program
recursion, syntax,  types, example program
recursion, syntax,  types, example program
recursion, syntax,  types, example program
recursion, syntax,  types, example program
recursion, syntax,  types, example program
recursion, syntax,  types, example program
recursion, syntax,  types, example program

recursion, syntax, types, example program

  • 1.
    RECURSION What is Recursionin C? Recursion is the process of a function calling itself repeatedly till the given condition is satisfied. A function that calls itself directly or indirectly is called a recursive function and such kind of function calls are called recursive calls. In C, recursion is used to solve complex problems by breaking them down into simpler sub-problems. We can solve large numbers of problems using recursion in C. For example, factorial of a number, generating Fibonacci series, generating subsets, etc.
  • 2.
    Recursive Functions inC In C, a function that calls itself is called Recursive Function. The recursive functions contain a call to themselves somewhere in the function body. Moreover, such functions can contain multiple recursive calls. Basic Structure of Recursive Functions The basic syntax structure of the recursive functions is: type function_name (args) { // function statements // base condition // recursion case (recursive call) }
  • 3.
    Example: C Programto Implement Recursion In the below C program, recursion is used to calculate the sum of the first N natural numbers. // C Program to calculate the sum of first N natural numbers // using recursion #include <stdio.h> int nSum(int n) { // base condition to terminate the recursion when N = 0 if (n == 0) { return 0; } // recursive case / recursive call }
  • 4.
    int res =n + nSum(n - 1); return res; } int main() { int n = 5; // calling the function int sum = nSum(n); printf("Sum of First %d Natural Numbers: %d", n, sum); return 0;
  • 5.
    Output Sum of First5 Natural Numbers: 15 Fundamentals of C Recursion The fundamental of recursion consists of two objects which are essential for any recursive function. These are: Recursion Case Base Condition
  • 7.
    1. Recursion Case Therecursion case refers to the recursive call present in the recursive function. It decides what type of recursion will occur and how the problem will be divided into smaller subproblems. The recursion case defined in the nSum() function of the above example is: int res = n + nSum(n - 1); The recursion case if often represented mathematically is a recurrence relation. For the above case: f(N) = N + f(N - 1); This recurrence relation is used for the complexity analysis of the method.
  • 8.
    2. Base Condition Thebase condition specifies when the recursion is going to terminate. It is the condition that determines the exit point of the recursion. Note: It is important to define the base condition before the recursive case otherwise, the base condition may never encountered and recursion might continue till infinity. Considering the above example again, the base condition defined for the nSum() function: if (n == 0) { return 0; } Now that the basic terminologies and fundamentals are out of the way, let’s move on to understand how the recursion works in C.
  • 9.
    Types of CRecursion In C, recursion can be classified into different types based on what kind of recursive case is present. These types are: • Direct Recursion -Head Recursion -Tail Recursion -Tree Recursion • Indirect Recursion