Tutorials Home   >   Programming Basics   >   What is Recursion?

What is Recursion?

When solving a problem in programming, sometimes the solution involves repeating the same steps in smaller versions of the same problem. This process is called recursion. Recursion is an important concept in programming that allows a function to call itself to solve a task.

Recursion is commonly used in tasks like calculating factorials, working with sequences, or traversing tree structures. Learning recursion helps you think differently about problem-solving.

In this explanation, you will learn:

  • What recursion is

  • How recursion works

  • Types of recursion

  • Examples in programming

  • Common mistakes learners make


1. Definition of Recursion

Recursion is the process where a function calls itself to solve a smaller version of the original problem.

Simple Definition:

Recursion is when a function solves a problem by calling itself with simpler input.

A recursive function usually has two key parts:

  1. Base case – stops the recursion

  2. Recursive case – function calls itself


2. How Recursion Works

A recursive function keeps calling itself until it reaches the base case. Once the base case is reached, the function stops calling itself, and the program starts returning values back through all previous calls.

Think of it like stacking plates:

  • Each function call is a plate added to the stack

  • The base case is when no more plates are added

  • Plates are removed in reverse order as the recursion ends


3. Base Case and Recursive Case

3.1 Base Case

The base case is the condition that stops the recursion. Without it, the function would call itself forever, causing a program crash.

Example:

def countdown(n):
if n == 0: # base case
print("Done")
else:
print(n)
countdown(n-1) # recursive case

Calling countdown(5) prints:

5
4
3
2
1
Done

3.2 Recursive Case

The recursive case is where the function calls itself to handle a smaller version of the problem.

In the example above, countdown(n-1) is the recursive case.


4. Examples of Recursion

4.1 Factorial

The factorial of a number n is the product of all numbers from 1 to n.

Mathematical formula:

n! = n × (n-1)!

Python Example:

def factorial(n):
if n == 1: # base case
return 1
else:
return n * factorial(n-1) # recursive case
print(factorial(5)) # Output: 120


4.2 Fibonacci Series

The Fibonacci series is a sequence where each number is the sum of the previous two.

Python Example:

def fibonacci(n):
if n == 0: # base case
return 0
elif n == 1: # base case
return 1
else:
return fibonacci(n-1) + fibonacci(n-2) # recursive case
print(fibonacci(6)) # Output: 8


4.3 Printing a List in Reverse

def print_reverse(lst):
if len(lst) == 0: # base case
return
else:
print(lst[-1])
print_reverse(lst[:-1]) # recursive case
print_reverse([1,2,3])

Output:

3
2
1

5. Types of Recursion

5.1 Direct Recursion

A function calls itself directly.

Example:

def greet():
print("Hello")
greet() # direct recursion

5.2 Indirect Recursion

A function calls another function, which eventually calls the first function.

Example:

def func1():
func2()
def func2():
func1()


6. Recursion in Different Languages

6.1 Python

def sum(n):
if n == 0:
return 0
else:
return n + sum(n-1)

6.2 Java

int factorial(int n) {
if(n == 1) return 1;
else return n * factorial(n-1);
}

6.3 C++

int factorial(int n) {
if(n == 1) return 1;
else return n * factorial(n-1);
}

7. Advantages of Recursion

  1. Makes code simpler and easier to read

  2. Helps solve problems that are naturally recursive, like trees and graphs

  3. Reduces the need for loops in some cases


8. Disadvantages of Recursion

  1. Uses more memory because of function call stack

  2. Can be slower than loops for simple problems

  3. Infinite recursion occurs if base case is missing, causing program crash


9. Common Mistakes Learners Make

  1. Forgetting the base case

  2. Using too many recursive calls for simple tasks

  3. Mixing recursion with incorrect logic

  4. Confusing recursion with loops


10. Best Practices

  • Always define a base case

  • Keep the recursive function simple

  • Test with small inputs first

  • Use recursion only when it simplifies the problem


11. Real-World Analogy

Think of recursion like Russian dolls:

  • Each doll contains a smaller doll inside it

  • You open one doll at a time until you reach the smallest

  • Then, you close the dolls back in reverse order


Conclusion

Recursion is when a function solves a problem by calling itself. It is a powerful tool in programming for breaking complex problems into smaller, simpler ones. By mastering recursion, learners can solve mathematical problems, navigate data structures, and write elegant code.

As you continue learning programming, remember: