RG's E-Learning Centre · Book a free demo class · WhatsApp +91-9379416374

Tracing recursive functions: Class 12 practice

A recursive function calls itself on a smaller input until it reaches a base case. The quick answer and an example come first; then the method, worked examples and six graded questions.

Tracing recursive functions: quick answer

A recursive function calls itself on a smaller input until it reaches a base case. Trace each call, then combine the returned values on the way back.

Example: def f(n): return 1 if n <= 1 else n * f(n - 1). What is f(4) and how many calls are made? Answer: 24, 4 calls.

  • Where it is taught: "Functions", Class 12 Computer Science.
  • On this page: the method, 3 worked examples and 6 graded questions with a full answer key.

How to use this page (about 30 minutes)

  1. Read the method once, then cover the worked examples and try them yourself before reading the steps.
  2. Do the two Basic questions without notes. If either is wrong, re-read the matching worked example.
  3. Do the Standard and Challenge questions, then check every line of working against the answer key — not only the final answer.
  4. Write down the mistake behind each wrong answer and redo that question the next day.

What you should know first

  • Functions

The idea behind tracing recursive functions

A recursive function calls itself on a smaller input until it reaches a base case. Trace each call, then combine the returned values on the way back.

Step-by-step method

  1. Find the base case.
  2. Write the chain of calls down to the base case.
  3. Multiply/add results while returning.

Worked example 1 (Basic)

def f(n): return 1 if n <= 1 else n * f(n - 1). What is f(4) and how many calls are made?

  1. f(4) = 4 × f(3) … f(1) = 1
  2. = 24; calls: 4
  3. Answer: 24, 4 calls

Worked example 2 (Standard)

def s(n): return 0 if n == 0 else n + s(n - 1). What is s(8)?

  1. s(8) = 8 + … + 1 + 0
  2. Answer: 36

Worked example 3 (Challenge)

def fib(n): return n if n < 2 else fib(n-1) + fib(n-2). What is fib(8), and how many calls does it make in total?

  1. fib(8) = 21
  2. Total calls = 67
  3. Answer: 21, 67 calls

Common mistakes and misconceptions

  • Missing the base case value
  • Counting the number of calls wrong

Practice questions

  1. (Basic) def f(n): return 1 if n <= 1 else n * f(n - 1). What is f(7) and how many calls are made?
  2. (Basic) def f(n): return 1 if n <= 1 else n * f(n - 1). What is f(3) and how many calls are made?
  3. (Standard) def s(n): return 0 if n == 0 else n + s(n - 1). What is s(7)?
  4. (Standard) def s(n): return 0 if n == 0 else n + s(n - 1). What is s(6)?
  5. (Challenge) def fib(n): return n if n < 2 else fib(n-1) + fib(n-2). What is fib(6), and how many calls does it make in total?
  6. (Challenge) def fib(n): return n if n < 2 else fib(n-1) + fib(n-2). What is fib(4), and how many calls does it make in total?

Answer key with full working

  • Q1: f(7) = 7 × f(6) … f(1) = 1 → = 5040; calls: 7 → Answer: 5040, 7 calls
  • Q2: f(3) = 3 × f(2) … f(1) = 1 → = 6; calls: 3 → Answer: 6, 3 calls
  • Q3: s(7) = 7 + … + 1 + 0 → Answer: 28
  • Q4: s(6) = 6 + … + 1 + 0 → Answer: 21
  • Q5: fib(6) = 8 → Total calls = 25 → Answer: 8, 25 calls
  • Q6: fib(4) = 3 → Total calls = 9 → Answer: 3, 9 calls

Which textbook this practice follows

This skill is taught under "Functions" in Class 12 Computer Science. Check your own textbook's contents page — CBSE, ICSE/ISC and state boards order chapters differently.

Answers on this page are generated and automatically re-checked by a second, independent calculation. They have not been reviewed by a teacher — if you spot a problem, tell us on WhatsApp.

Still stuck after the answer key?

If the same step in tracing recursive functions keeps going wrong, a short live 1 on 1 class can find the exact gap. The first demo class is free; send the class, board and chapter on WhatsApp (+91 9379416374).

Related practice and notes

More Class 12 Computer Science practice

Continue with this syllabus

Frequently asked questions

Which chapter is tracing recursive functions in?

This skill is taught under "Functions" in Class 12 Computer Science. Check your own textbook's contents page — CBSE, ICSE/ISC and state boards order chapters differently.

Which question should I start with?

Start with the Basic questions. Move to Standard only when both Basic answers match the key with the same working; the Challenge questions combine this skill with earlier ones.

How were the answers checked?

Answers on this page are generated and automatically re-checked by a second, independent calculation. They have not been reviewed by a teacher — if you spot a problem, tell us on WhatsApp.

Is this only for CBSE?

The skill names follow NCERT/CBSE, but the method is the same for ICSE, state boards, IGCSE and IB. Only the chapter name and order differ.

Can I get live help with this topic?

Yes. RG's E-Learning teaches live 1 on 1 online classes; the first demo class is free and fees are shared after it.

Book a free demo class