---
title: "Direct and indirect recursion"
subject: "Data Structure and Algorithm"
chapter: 5
key: "semester_4/data_structure_and_algorithm:direct-indirect-recursion"
source: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/direct-indirect-recursion
site: BCT Engineering
---

# Direct and indirect recursion

Data Structure and Algorithm · Chapter 5: Recursion

- Asked in: 2071 Magh, 4 marks
- Video: [Types of Recursion (Part 1) | Direct & Indirect Recursion](https://www.youtube.com/watch?v=t9whckmAEq0) — Neso Academy
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/direct-indirect-recursion
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-5

## Question

Explain direct and indirect recursion with suitable example.

## Short answer

Direct recursion is a function calling itself, for example fact(n)=n*fact(n-1). Indirect recursion occurs when a function calls another function that eventually calls the first, forming a cycle such as isEven->isOdd->isEven. Direct is easier to trace; indirect is harder.

## Answer

#### Direct recursion
A function calls **itself** directly in its own body.
```c
int fact(int n) {
    if (n == 0) return 1;
    return n * fact(n - 1);     /* fact calls fact */
}
```
Call chain: `fact → fact → fact ...`

#### Indirect recursion
A function calls another function, which in turn calls the first one again, forming a cycle (`A → B → A`). The self-call happens through other functions.
```c
void funB(int n);
void funA(int n) {
    if (n > 0) { printf("%d ", n); funB(n - 1); }
}
void funB(int n) {
    if (n > 1) { printf("%d ", n); funA(n / 2); }
}
```
Call chain: `funA → funB → funA → ...` For `funA(20)` the output is `20 19 9 8 4 3 1`.

| Direct | Indirect |
|---|---|
| Function calls itself | Calls through one or more other functions |
| Easy to understand and trace | Harder to trace |
| Example: factorial | Example: isEven/isOdd calling each other |

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Which describes direct recursion?
   A. A function calls another function only
   B. A function is called by main only
   C. Two functions call each other
   D. A function calls itself in its own body
   Answer: D. Direct recursion is the self-call. (Key point: Direct recursion)

2. Which describes indirect recursion?
   A. A calls B and B calls A again (a cycle)
   B. A calls A
   C. A calls B and B never returns
   D. A calls main
   Answer: A. The cycle A to B to A is indirect. (Key point: Indirect recursion)

3. Which is an example of indirect recursion?
   A. fact calling fact
   B. isEven and isOdd calling each other
   C. sum calling sum
   D. fib calling fib
   Answer: B. Each function calls the other. (Key point: Example)

4. How do direct and indirect recursion compare?
   A. Direct is harder to trace
   B. Both are identical to trace
   C. Indirect is harder to trace
   D. Indirect needs no base case
   Answer: C. Following the cycle across functions is harder. (Key point: Tracing)

## Related in Data Structure and Algorithm

- [Define recursion; direct vs indirect](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adefine-recursion-direct-indirect) — Data Structure and Algorithm, chapter 5

## Related in other subjects

- [Degenerate semiconductors; direct vs indirect bandgap](https://www.bctengineer.com/question.md?key=semester_3%2Felectrical_engineering_material%3Adegenerate-direct-indirect) — Electrical Engineering Material, chapter 6
- [Direct versus indirect band gap semiconductors](https://www.bctengineer.com/question.md?key=semester_3%2Felectrical_engineering_material%3Adirect-indirect-bandgap) — Electrical Engineering Material, chapter 6
- [Direct and indirect recombination](https://www.bctengineer.com/question.md?key=semester_3%2Felectrical_engineering_material%3Arecombination-direct-indirect) — Electrical Engineering Material, chapter 6
- [Direct, indirect, deflection and null methods](https://www.bctengineer.com/question.md?key=semester_4%2Finstrumentation_and_measurement%3Afundamental-measurement-methods) — Instrumentation and Measurement, chapter 1
