Short Notes on Recursion

Last Updated : 21 Feb, 2026

Recursion is a programming technique where a function calls itself to solve a smaller or simpler version of the original problem until a base case is reached.

How Recursion Works :

  • Each recursive call is pushed onto the call stack.
  • When the base case is reached, the calls start returning in reverse order.
  • Think of it as “divide the problem → solve smaller problem → combine results”

Steps to Implement Recursion :

Step1 - Define a base case: Identify the simplest (or base) case for which the solution is known or trivial. This is the stopping condition for the recursion, as it prevents the function from infinitely calling itself.

Step2 - Define a recursive case: Define the problem in terms of smaller subproblems. Break the problem down into smaller versions of itself, and call the function recursively to solve each subproblem.

Step3 - Ensure the recursion terminates: Make sure that the recursive function eventually reaches the base case, and does not enter an infinite loop.

Step4 - Combine the solutions: Combine the solutions of the subproblems to solve the original problem.

Examples:

Sum of first n Natural Numbers


C++
int sum(int n)
{
    // base case
    if(n == 1)
        return 1;
        
    // recursive case
    return n + sum(n - 1);
}
Java
static int sum(int n)
{
    // base case
    if(n == 1)
        return 1;

    // recursive case
    return n + sum(n - 1);
}
Python
def sum(n):
    # base case
    if(n == 1):
        return 1

    # recursive case
    return n + sum(n - 1)
C#
static int sum(int n)
{
    // base case
    if(n == 1)
        return 1;

    // recursive case
    return n + sum(n - 1);
}
JavaScript
function sum(n)
{
    // base case
    if(n == 1)
        return 1;

    // recursive case
    return n + sum(n - 1);
}

Time Complexity: O(n) - The function makes n recursive calls.
Space Complexity: O(n) - n function calls are stored in the call stack.

Factorial Execution

dsa
C++
int fact(int n)
{
    // base case
    if(n == 0)
        return 1;
        
    // recursive case    
    return n * fact(n - 1);
}
Java
static int fact(int n)
{
    // base case
    if(n == 0)
        return 1;

    // recursive case    
    return n * fact(n - 1);
}
Python
def fact(n):
    # base case
    if(n == 0):
        return 1

    # recursive case    
    return n * fact(n - 1)
C#
static int fact(int n)
{
    // base case
    if(n == 0)
        return 1;

    // recursive case    
    return n * fact(n - 1);
}
JavaScript
function fact(n)
{
    // base case
    if(n == 0)
        return 1;

    // recursive case    
    return n * fact(n - 1);
}

Time Complexity: O(n) - The function calls itself n times.
Space Complexity: O(n) - The recursion depth is n and each call uses stack memory.

Fibonacci Series

fibnaccci
C++
int fib(int n)
{
    // base case
    if(n == 0) return 0;
    if(n == 1) return 1;
    
    // recursive call
    return fib(n-1) + fib(n-2);
}
Java
static int fib(int n)
{
    // base case
    if(n == 0) return 0;
    if(n == 1) return 1;

    // recursive call
    return fib(n-1) + fib(n-2);
}
Python
def fib(n):
    # base case
    if(n == 0):
        return 0
    if(n == 1):
        return 1

    # recursive call
    return fib(n-1) + fib(n-2)
C#
static int fib(int n)
{
    // base case
    if(n == 0) return 0;
    if(n == 1) return 1;

    // recursive call
    return fib(n-1) + fib(n-2);
}
JavaScript
function fib(n)
{
    // base case
    if(n == 0) return 0;
    if(n == 1) return 1;

    // recursive call
    return fib(n-1) + fib(n-2);
}

Time Complexity: O(2n) - Each function call makes two recursive calls, forming an exponential recursion tree.
Space Complexity: O(n) - The maximum recursion depth is n.

Recursion Vs Iteration :

recursion_vs_iteration

Also Check:

Comment