Recursion - Explained in the Easiest Way

Learn to Code in Recursion in the easiest way

In this article, we will discover what is recursion, what are the steps and how problems can be solved using recursive methods. We will also see what are the steps involved while solving a recursive problem.

First, let's understand the memory structure of a programming language.


Stack Memory and Heap Memory

Memory structures in programming languages are vital for managing and organizing program data. They store and allow access to variables, objects, instructions, and other components during runtime.

Memory is divided into two areas called Stack Memory and Heap Memory.

Since we are focusing on recursion I will just give you a brief about both types of memory.

Stack memory:

  • It stores method calls, local variables, and object references using a Last-In-First-Out (LIFO) approach.

  • It's efficient for managing local variables and method calls with fast access and automatic memory management.

  • JVM manages stack memory, and it's not directly accessible to programmers.

Heap memory:

  • It is used for dynamic memory allocation, primarily for objects and data structures in Java.

  • The garbage collector manages memory allocation and deallocation in the heap, automatically reclaiming memory for unreferenced objects.

  • Heap memory offers flexibility by allowing non-sequential allocation and deallocation of objects.

A Simple Program to Understand the Working of Stack for Functions

Now we will a simple program where we will use functions to understand how the stack works for function.

public static void main(String[] args) {
        message();
    }

    static void message() {
        System.out.println("Hello World");
        message1();
    }

    static void message1() {
        System.out.println("Hello World");
        message2();
    }

    static void message2() {
        System.out.println("Hello World");
        message3();
    }

    static void message3() {
        System.out.println("Hello World");
        message4();
    }

    static void message4() {
        System.out.println("Hello World");
    }

For the above program, let's see how the stack gets filled up and empty. And remember these two things are very important:

  • Until and unless the work of the particular function in the stack is not completed it remains in the stack.

  • And when the function is removed from the stack the flow of the program is restored to where the function was called.

Let's see at each step of the stack what was the output.

Now that we have understood how the stack deals with function let's jump into what is recursion and how it works.

What is Recursion?

Recursion is a programming concept where a function calls itself until it reaches a specific condition, known as the base case. Recursion is a process of solving a problem by breaking it down into smaller, similar subproblems.

Now the programs we run on our computers are using the RAM for the stack memory. If we keep on calling the memory may get filled up and it will show an error called StackOverFlowError. It means there is no more space left in the memory to store new functions coming up.

Why Use Recursion?

  • It helps us simply solve bigger/more complex problems.

  • You can convert the recursive solution into iteration and vice-versa.

  • Space complexity is not constant because of recursive calls. Because the function stores up memory in the stack.

  • We can break down our bigger problems into smaller problems.

Visualising Recursion using Recursion Tree

The function calls that happen repeatedly using recursion are seen by something called a recursion tree.

Let's visualise for the above program:

Thank You! I hope you find this helpful.