---
title: "Recursive function and how it works"
subject: "Computer Programming"
chapter: 6
key: "semester_1/computer_programming:recursive-function-explain"
source: https://www.bctengineer.com/q/semester_1/computer_programming/recursive-function-explain
site: BCT Engineering
---

# Recursive function and how it works

Computer Programming · Chapter 6: User-Defined Functions

- Asked in: 2071 Shrawan, 2+2 marks; 2071 Chaitra, 2 marks; 2074 Asoj, 2 marks (asked 3 times)
- Video: [Recursion in C](https://www.youtube.com/watch?v=kepBmgvWNDw) — Neso Academy
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/recursive-function-explain
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-6

## Question

What is a recursive function? How does it work? Give an example of recursive function. When can we use recursive functions?

## Short answer

A recursive function calls itself with a smaller input until a base case stops recursion. It uses stack memory for each call. Example: factorial (n * factorial(n-1)). Use it when a problem decomposes into similar subproblems.

## Answer

#### Recursive function
A **recursive function** is a function that calls itself, directly or indirectly, to solve a smaller version of the same problem.

#### How it works
- It must have a **base case**, a condition that stops the recursion; otherwise it never ends.
- It has a **recursive case** which calls itself with a smaller (simpler) argument.
- Each call gets its own set of local variables on the stack. When the base case is reached, the calls return one after another in reverse order, and the results are combined.

#### Example: factorial
```c
#include <stdio.h>
long factorial(int n)
{
    if (n <= 1)                 /* base case */
        return 1;
    return n * factorial(n - 1);   /* recursive case */
}
int main(void)
{
    int n = 5;
    printf("%d! = %ld\n", n, factorial(n));
    return 0;
}
```
Output:
```text
5! = 120
```

```text
factorial(3) = 3 * factorial(2)
                    = 2 * factorial(1)
                          = 1       <- base case
factorial(2) = 2 * 1 = 2
factorial(3) = 3 * 2 = 6
```

#### When to use recursion
When a problem can be divided into similar smaller problems: factorial, Fibonacci series, GCD, power, Tower of Hanoi, binary search, tree traversal and sorting methods like quick sort.

## Check yourself

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

1. Recursive function?
   A. Calls main
   B. Has no return
   C. Uses loops
   D. Calls itself to solve smaller version
   Answer: D. Definition. (Key point: Definition)

2. Why base case?
   A. Stops recursion
   B. Faster
   C. Declares
   D. Returns void
   Answer: A. Otherwise never ends. (Key point: Base case)

3. How are locals handled?
   A. Shared
   B. Each call has own set on stack
   C. Global
   D. Heap only
   Answer: B. Frames. (Key point: Stack)

4. When use recursion?
   A. Never
   B. Simple arithmetic
   C. Problem splits into similar smaller problems
   D. Printing
   Answer: C. E.g. factorial, Hanoi. (Key point: Use)

## Related in Computer Programming

- [Series (2*3/5)+(4*5/7)+… with functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aseries-2x3-over-5-recursive) — Computer Programming, chapter 6
- [Function definition; sum of two numbers](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afunction-definition-sum-two) — Computer Programming, chapter 6
- [Reverse a string using functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astring-reverse-with-count-function) — Computer Programming, chapter 7, asked 2×
- [Complex number sum via functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-complex-sum-function) — Computer Programming, chapter 8
- [Pass student array; display passed students](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-whole-array-student-info) — Computer Programming, chapter 7
- [Armstrong number using recursion](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarmstrong-recursive) — Computer Programming, chapter 6

## Related in other subjects

- [Normal vs recursive function](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Anormal-vs-recursive-function) — Data Structure and Algorithm, chapter 5
- [Photoelectric effect: work function of sodium](https://www.bctengineer.com/question.md?key=semester_3%2Felectrical_engineering_material%3Aphotoelectric-sodium-work-function) — Electrical Engineering Material, chapter 1, asked 2×
- [Function of painting work](https://www.bctengineer.com/question.md?key=semester_6%2Fbuilding_technology%3Afunction-of-painting) — Building Technology, chapter 9
- [Is recursion slow; recursive vs non-recursive](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Arecursion-slow-compare) — Data Structure and Algorithm, chapter 5
