MCS-201 · Unit 5

Functions

Section 8 of 8

Block 2 · Unit 5Functions14 min · Section 8 of 8

5.8Recursion

Covers IGNOU MCS-201 §5.9

Some problems are naturally defined in terms of smaller versions of themselves: the factorial of n is n times the factorial of n − 1. Recursion lets a function express such a definition directly by calling itself. It is a powerful technique, and the basis of many algorithms in later courses.

What recursion is

Every recursive function needs two parts:

  1. A base case (terminating condition): an input for which the answer is known directly.
  2. A recursive case: the function calls itself with an argument that is closer to the base case.

Without a reachable base case, the calls never stop. Each call uses memory on the call stack, so the program eventually crashes with a stack overflow.

Factorial: from loop to recursion

The mathematical definition is recursive:

n!={1if n≤1n×(n−1)!if n>1n! = \begin{cases} 1 & \text{if } n \le 1 \\ n \times (n-1)! & \text{if } n > 1 \end{cases}

Power by recursion

aba^b has the same shape: a0=1a^0 = 1 and ab=a×ab−1a^b = a \times a^{b-1}.

Fibonacci series

The Fibonacci series starts 1, 1, and each later term is the sum of the two before it: 1, 1, 2, 3, 5, 8, 13, …

Recursion versus iteration

AspectRecursionIteration (loops)
MechanismA function calls itselfA loop repeats statements
Stops whenThe base case is reachedThe loop condition becomes false
MemoryA new set of variables on the stack for every callA fixed set of variables
SpeedSlower: each call has overheadFaster
RiskStack overflow if the base case is missed or the depth is very largeInfinite loop if the condition never becomes false
CodeShort and close to the mathematical definitionSometimes longer, but efficient
Best forNaturally recursive problems: trees, Towers of Hanoi, divide-and-conquerSimple repetition and counting

Anything written with recursion can also be written with loops, and the reverse. Choose the form that is clearest; for simple counting problems such as factorial, the loop is more efficient.

Key takeaways

  • Recursion: a function calls itself to solve a smaller version of the problem.
  • Every recursive function needs a base case and a recursive case that approaches it.
  • Calls stack up until the base case, then return in reverse order.
  • Recursion is elegant but uses more memory and time than a loop.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What is the purpose of the base case in a recursive function?

Show answer

Answer: B. To stop the recursion

The base case gives an answer without a further call, which ends the chain of calls. Without it, the calls continue until the stack overflows.

Question 2 · multiple choice

How many times is fact called in total when computing fact(5) with the base case n <= 1?

Show answer

Answer: D. 5

The calls are fact(5), fact(4), fact(3), fact(2) and fact(1): five calls. 120 is the result, not the number of calls.

Question 3 · multiple choice

What does this program print?

C · sum_rec.c
#include <stdio.h>

int sum(int n)
{
    if (n == 0)
        return 0;
    return n + sum(n - 1);
}

int main(void)
{
    printf("%d\n", sum(4));
    return 0;
}
Show answer

Answer: C. 10

sum(4) = 4 + sum(3) = 4 + 3 + 2 + 1 + 0 = 10. The value 24 would be the product (factorial), not the sum.

Question 4 · multiple choice

What happens if a recursive function has no reachable base case?

Show answer

Answer: C. The calls continue until the stack overflows and the program crashes

Each call uses stack memory; with no stopping point, the memory runs out. The compiler cannot detect this in general.

Question 5 · multiple choice

What does this program print?

C · countdown_rec.c
#include <stdio.h>

void show(int n)
{
    if (n == 0)
        return;
    printf("%d ", n);
    show(n - 1);
}

int main(void)
{
    show(3);
    printf("\n");
    return 0;
}
Show answer

Answer: A. 3 2 1

Each call prints its n before calling itself with n - 1, so the numbers appear in decreasing order; when n is 0 the function returns without printing.

Question 6 · multiple choice

Compared with a loop, a recursive solution usually:

Show answer

Answer: D. Uses more memory, because every call has its own variables on the stack

Each active call keeps its own parameters and local variables until it returns, so deep recursion uses more memory than a loop.

Question 7 · short answer

Write a recursive function sum_digits that returns the sum of the digits of a non-negative integer, and trace it for 472.

Show worked answer

int sum_digits(int n) { if (n == 0) return 0; return n % 10 + sum_digits(n / 10); }. Base case: 0 has digit sum 0. Recursive case: the last digit n % 10 plus the digit sum of the remaining number n / 10. Trace: sum_digits(472) = 2 + sum_digits(47); sum_digits(47) = 7 + sum_digits(4); sum_digits(4) = 4 + sum_digits(0); sum_digits(0) = 0. Returning: 4 + 0 = 4; 7 + 4 = 11; 2 + 11 = 13. The result is 13.

↑ ↓ move↵ openTab switch type