C Recursion (original) (raw)

Last Updated : 13 May, 2025

**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.

**Example:

C `

#include <bits/stdc++.h> using namespace std;

void rec(int n){ if(n == 6) return; cout << "Recursion Level " << n << endl; rec(n+1); }

int main() { rec(1); return 0; }

`

Output

Recursion Level 1 Recursion Level 2 Recursion Level 3 Recursion Level 4 Recursion Level 5

In the above example, we write a function called **rec() that takes n as input and print ****"Recursion Level"** corresponding with n. Then this function increments the value of nand call itself till n ==6, then it stops.

Recursive Functions

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.

**Example

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.

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. It is important to define the base condition before the recursive case otherwise, the base condition may never encounter and recursion might continue till infinity

How Recursion works?

To understand how C recursion works, let's take another example and see base condition or recursive case:

C `

#include using namespace std;

int nSum(int n) {

// Base condition to terminate
// recursion when N = 0
if (n == 0)
    return 0;

// recursive case / recursive call
int res = n + nSum(n - 1);
return res;

}

int main() {

// Calling the function
int sum = nSum(5);

cout << sum;
return 0;

}

`

In the above example,

**Base Condition

C `

if (n == 0) return;

`

**Recursive Case

C `

int res = n + nSum(n – 1);

`

Let’s see how recusion unfolds:

The below image lists the recursive case for each of the recursive call.

c recursion tree

Recursion Tree Diagram of nSum(5) Function

Memory Management in Recursion

Like other functions, the data of a recursive function is stored in stack memory as a stack frame. This stack frame is removed once the function returns a value. In recursion,

Refer this article to know more - Function Call Stack

**Stack Overflow

The program's call stack has limited memory assigned to it by the operating system and is generally enough for program execution. But if we have a recursive function that goes on for infinite times, sooner or later, the memory will be exhausted, and no more data can be stored. This is called stack overflow. In other words,

Stack overflow is the error that occurs when the call stack of the program cannot store more data resulting in program termination.

Types of Recursions in C

In C, recursion can be classified into different types based on what kind of recursive case is present. These types are:

  1. **Direct Recursion
    • **Head Recursion
    • **Tail Recursion
    • **Tree Recursion
  2. **Indirect Recursion

Types-of-Recursion

Types of Recursion in C

1. Direct Recursion

Direct recursion is the most common type of recursion, where a function calls itself directly within its own body. The recursive call can occur once or multiple times within the function due to which we can further classify the direct recursion

**A. Head Recursion

The head recursion is a linear recursion where the position of its only recursive call is at the start of the function. It is generally the first statement in the function.

**B. Tail Recursion

The tail recursion is also a liner recursion like head recursion, but the position of the recursive call is at the end of the function. Due to this, the tail recursion can be optimized to minimize the stack memory usage. This process is called Tail Call Optimization.

In the first example, the nSum() does the tail recursion.

**C. Tree Recursion

In tree recursion, there are multiple recursive calls present in the body of the function. Due to this, while tracing the program flow, it makes a tree-like structure, hence the name Tree Recursion.

2. Indirect Recursion

Indirect recursion is an interesting form of recursion where a function calls another function, which eventually calls the first function or any other function in the chain, leading to a cycle of function calls. In other words, the functions are mutually recursive. This type of recursion involves multiple functions collaborating to solve a problem.

**Practical Examples

The following examples will improve the understanding of C++ recursion:

Reverse a String

C `

#include <stdio.h> #include <string.h>

void reverseString(char str[], int start, int end) {

// Base condition to stop
if (start >= end) return;

// Swap characters at start and end
char temp = str[start];
str[start] = str[end];
str[end] = temp;

// Recursively call the function for the next pair
reverseString(str, start + 1, end - 1);

}

int main() { char str[] = "Hello, Geek"; int length = strlen(str); printf("Original String: %s\n", str); reverseString(str, 0, length - 1); printf("Reversed String: %s\n", str); return 0; }

`

Output

Original String: Hello, Geek Reversed String: keeG ,olleH

Power of a Number

C `

#include <stdio.h>

int power(int n, int m) {

// Base condition to stop recursion
if (m == 0) return 1;

// Recursive case
return n * power(n, m - 1);  

}

int main() { int n = 2, m = 3; int result = power(n, m); printf("%d raised to the power of %d is: %d\n", n, m, result); return 0; }

`

Output

2 raised to the power of 3 is: 8

Applications of Recursion in C

Recursion is widely used to solve different kinds of problems from simple ones like printing linked lists to being extensively used in AI. Some of the common uses of recursion are:

Advantages of C Recursion

The advantages of using recursive methods over other methods are:

Disadvantages of C Recursion

As with almost anything in the world, recursion also comes with certain limitations some of which are:

  1. Recursive functions make our program a bit slower due to function call overhead.
  2. Recursion functions always take extra space in the function call stack due to separate stack frames.
  3. Recursion methods are difficult to understand and implement.

Recursion vs Iteration

Recursion and iteration both involve repeating a set of instructions. The key difference between them is as follows:

To know more difference, refer this article - Difference between Recursion and Iteration