Introduction to Recursion.

Dipak JadhavDipak Jadhav
3 min read

This blog is about Recursion in DSA. Let's understand it.

Hello everyone, you tap on this blog it means that you are interested in DSA. Then today you will be familiar with an important topic that is Recursion.

  • Before we understand" What is Recursion ", Firstly let's understand how the function call makes in the programs.
public class recursion{

public static void main(String[] args){
func1();
}
public static void func1(){
System.out.println("I am function 1");
func2();
}
public static void func2(){
System.out.println("I am function 2");
func3();
}
public static void func3(){
System.out.println("I am function 3");
}
}

From the above, what do you understand? What is flow of execution of functions? And also what is the output?

  • Here is some interesting part about the functions :
  1. Until the function is not finished execution, it will remain in the stack

    It means that the first function is to start execution in "main"(every program starts with main function in java). After that in the main function, there is func1() is called So the program flow goes toward the function 1, after that function 2, and lastly function 3.

  2. Remember that, After a function finishes execution, it is removed from the stack and the flow of program is given to where it is called. (Note, it is important to remember)

  3. Here we are doing repeated work calling function from one to another. And for a large amount of work, we can't do these kinds of things.

  • To solve this kind of repeated things to do, here is recursion comes to help us.

  • We don't want to write that much code, it makes it simple to do.

Base condition in Recursion :

  • Condition where our recursion will stop making new calling

  • Every call by recursion takes every separate memory.

  • If No base condition, then function calls will keep happening, stack will be filled again and again ....

  • Memory of computer will exceed the limit, And is called stack overflow error (there is website also called stack overflow).

  • Let's solve one question

// Print the 1 2 3 4 5 sequence 
public class Main {
    public static void main(String[] args) {

        func(5);
    }
    public  static void func(int n){

// base condition is important if we are not using then i told we will get stack overflow error!
        //basically it is an stoping condition.
        if(n==1){
            return;
        }
        //it will print the number.
        System.out.print(n);
        func(n-1);
//  System.out.print(n);
    }
}
  • We are giving firstly base condition, that is if the n is equal to 1 then it should be return.

We are calling the same function, i.e. func(n-1). By every call, it will decrement by 1.

" Don't put " n--" instead of "n-1 " here,because ,some of the increment , decrement concepts. We will talk in the another blog if you want."

  • Before function call we are printing the number, it goes to 5-4-3-2-1. At 1 it will return;

Why Recursion?

  1. It helps us in solving bigger complex problem in a simple way.

  2. You can convert recursion solution into iteration and vice versa.

  3. It helps us in breaking down bigger problems into smaller ones.

So, I hope you are familiar with recursion. If this blog gets good response, I will definitely extend the recursion concepts, problems, steps to solve, etc.

Thank you for giving your precious time to read this blog

1
Subscribe to my newsletter

Read articles from Dipak Jadhav directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dipak Jadhav
Dipak Jadhav

I am a Web Developer and Tech Enthusiast Freak. Love to learn in public and with Collaboration. I am a 3rd year IT student.