On this page
If your practical file has a loop-based factorial program, rewrite it with the function calling itself and you have recursion. On paper, it is just a pile of unfinished calls.
What is recursion in Python?
A recursive function is a function that calls itself, each time on a smaller input, until it reaches a case it can answer directly. That direct case is the base case. The line that calls the function again is the recursive case.
Factorial is the usual first example because its maths definition is already recursive: n! = n × (n − 1)! (see why 0 factorial equals 1 for the bottom of that chain). In Python:
def factorial(n):
if n <= 1, return 1 (the base case)
otherwise, return n * factorial(n - 1) (the recursive case)
How does the call stack work for factorial(4)?
When a function calls another, Python pauses the first and keeps it in a frame on the call stack, newest on top. A recursive call works the same way. Here is factorial(4), traced:
factorial(4) needs factorial(3) before it can multiply, so it pauses. Stack: one frame.
factorial(3) needs factorial(2), and pauses. Two frames.
factorial(2) needs factorial(1), and pauses. Three frames.
factorial(1) hits the base case and returns 1 without calling anything. Four frames: the deepest point.
factorial(2) resumes: 2 × 1 = 2, and its frame comes off.
factorial(3) resumes: 3 × 2 = 6.
factorial(4) resumes: 4 × 6 = 24, and no factorial frames are left on the stack.
The multiplication happens on the way back up, not on the way down.
How does recursion work for Fibonacci?
fib(n) returns n if n < 2, otherwise fib(n - 1) + fib(n - 2). Two calls per step make a tree, not a chain.
The Python language reference says that Python evaluates expressions from left to right, so in fib(n - 1) + fib(n - 2) the left call, and everything beneath it, finishes before the right call starts. Traced for fib(4):
fib(4) calls fib(3) first.
fib(3) calls fib(2), which calls fib(1) and fib(0): 1 + 0 = 1.
fib(3) then calls fib(1), which returns 1, so fib(3) = 1 + 1 = 2.
Back in fib(4), the right side calls fib(2) again: 1 again.
fib(4) = 2 + 1 = 3.
Count the boxes and you get 9 calls for fib(4), with fib(2) worked out in two branches. The repeated work grows quickly with n; add a counter and print it for fib(10) to see.
Is recursion in the CBSE Class 12 syllabus?
The CBSE Computer Science (083) syllabus for 2026-27 does not list recursion by name. Its Class 12 Unit 1 does list functions, flow of execution, scope of a variable, and the stack data structure with push and pop. Those are the parts recursion is built from. The Class 11 part of the same document already suggests a factorial program under loops and lists a Fibonacci series program among its practicals.
So treat recursion as a deeper look at flow of execution and stacks, not an extra chapter. Ask your teacher whether it will come up in class tests or the viva, and do not take time from listed topics such as file handling or SQL for it.
What are the common recursion errors?
No base case, or one that is never reached. factorial(-1) with a base case of n == 0 keeps calling forever. Python stops it: the official docs say RecursionError is raised when the interpreter detects that the maximum recursion depth is exceeded. You can see the current limit with sys.getrecursionlimit().
Forgetting return. Writing n * factorial(n - 1) without return gives back None, because the Python tutorial notes that falling off the end of a function also returns None. The caller then multiplies a number by None and crashes with a TypeError.
An input that does not shrink. factorial(n) calling factorial(n) never ends.
The wrong base value. Returning 0 for factorial makes every answer 0.
Printing instead of returning. print shows a value but hands None back.
Our five-step debugging routine for beginners works well here.
A five-minute self-check before you run it
Name the base case and what it returns.
Check every recursive call moves closer to it.
Check every branch ends in return.
Trace the smallest input that is not a base case, as boxes on paper.
Run it with print(n) as the first line and watch the stack go down and come back up.
If frames and scope still feel shaky, the Fermi Concept Library has every concept in computing with its own lesson, so you can revisit functions before recursion. When your trace and your output disagree, the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning rather than printing an answer.
Once factorial feels easy, try writing the halving search from guessing a number from 1 to 100 as a recursive function.



