Skip to study material
← Question index

QUESTION 44 · Recursion

Compare recursion and iteration with reference to factorial.

5 marks

Answer in brief

Recursion solves repeated work through calls to smaller problems. Iteration performs repeated work using a loop.

Recursion

Recursive factorial returns 1 at its base case and otherwise returns n × factorial(n - 1). Each active call waits for the smaller result. The definition closely follows the mathematical relationship. With n levels it takes O(n) time and O(n) auxiliary space in the usual call-record model.

Iteration

Iterative factorial starts result at 1 and multiplies it by each value from 2 through n. There is no chain of pending recursive calls. It also takes O(n) time, because the number of multiplications grows linearly. Only a counter and result are needed, giving O(1) auxiliary space.

Comparison

  1. Stopping: recursion uses a base case; iteration uses a loop condition or an explicit exit.
  2. Stored state: recursion can keep separate state in active calls; iteration uses explicit variables.
  3. Readability: recursive problems such as tree traversal may be easier to describe recursively.
  4. Limits: deep recursion can exhaust call resources; a loop can still be wrong or non-terminating if its progress rule is faulty.

Which should be chosen?

For a simple factorial, iteration usually avoids unnecessary call depth. For naturally recursive structures, recursion can be clearer. Neither form is always faster or always better. Count the actual work and stored state. C does not guarantee tail-call optimisation, so it cannot be assumed to reduce every recursive function to constant extra space.

Correctness

Both forms must handle 0! = 1, reject unsupported negative input and avoid overflowing the result type. Changing recursion to a loop does not remove arithmetic limits.

Comparison table

↔ Scroll horizontally on a small screen
Factorial: recursion and iteration
FeatureRecursionIteration
RepetitionSmaller function callsLoop iterations
StoppingBase caseLoop condition
TimeO(n)O(n)
Auxiliary spaceO(n) active callsO(1) variables

Related exam questions

Related questions in the recursion category from the supplied papers. The marks below are the printed allocations, including any combined subquestion.

  • September/October 2021 · Q8(a) · 7 marks

    Explain recursion with an example program.

    Paper P01 · page 1
  • March/April 2023 · Q9(b) · 5 marks

    What is recursion? State its advantages and disadvantages.

    Paper P02 · page 2

View this category in the exam question bank →