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
int sum(int n)
{
// base case
if(n == 1)
return 1;
// recursive case
return n + sum(n - 1);
}
static int sum(int n)
{
// base case
if(n == 1)
return 1;
// recursive case
return n + sum(n - 1);
}
def sum(n):
# base case
if(n == 1):
return 1
# recursive case
return n + sum(n - 1)
static int sum(int n)
{
// base case
if(n == 1)
return 1;
// recursive case
return n + sum(n - 1);
}
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

int fact(int n)
{
// base case
if(n == 0)
return 1;
// recursive case
return n * fact(n - 1);
}
static int fact(int n)
{
// base case
if(n == 0)
return 1;
// recursive case
return n * fact(n - 1);
}
def fact(n):
# base case
if(n == 0):
return 1
# recursive case
return n * fact(n - 1)
static int fact(int n)
{
// base case
if(n == 0)
return 1;
// recursive case
return n * fact(n - 1);
}
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

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);
}
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);
}
def fib(n):
# base case
if(n == 0):
return 0
if(n == 1):
return 1
# recursive call
return fib(n-1) + fib(n-2)
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);
}
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 :
