What is Recursion? – explained in JAVA

Have you ever had a dream, in which you dreamed that you were dreaming and in that one you were dreaming and in … and so on? That’s what we call Recursion.

In this article, I will explain what recursion is in programming. I’m going to do it with Java, but the concept is the same for any language. Firstly we will see what recursion is and then implement several examples.

What is recursion?

It is a technique used in programming that allows a block of instructions to be executed a certain number of times (the one we determine). Sometimes it’s complicated to understand, but don’t worry. When we look at the examples, it will be clear. In Java, as in many other languages, methods can call themselves. Thanks to this, we can use recursion in our favor instead of iteration to solve certain types of problems.

What is Recursion?Simple example

Let’s see a small example that does absolutely nothing :P . It is a method whose sole aim is to call itself:

Void countRecursion() {
     countRecursion();
}

If you execute this, It will give an error on the stack (mythical StackOverflow error, Bible programmers).

As you can see, I have called the method countRecursion because we will show an on-screen countdown of a number that we pass as a parameter to the function. For example, to count 20 without recursion, we can do:

for (int i = 20; i> = 0; i--) {
System.out.println(i);
}

Now, to do it recursively, we would have to pass a number as a parameter. Also, after printing that number, we will call the same function with the current number by subtracting one:

void countRecursion(int number) {
   system.out.println(number);
  countRecursion(number - 1);
}

This is what I said above. We call the function a 10. We print the 10 and call the function a 9. We print the 9 and call the function with an 8. So until the end of the day. I say until the end of the day because you will miss an error if you execute this directly:

public class Recursivity {

    static void countRecursion(int number) {
    System.out.println(number);
    countRecursion(number - 1);
    }

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

Problem: infinite call. For this, what we have to do is that when the number is 0, stop calling the function. For that, we put one conditional structure of life. Thanks to the conditional, it will stop running after 0:

void countRecursion(int number) {
     System.out.println (number);
       If (number> 0) {
   countRecursion(number - 1);
   }
}

Factorial

Calculating the factorial of a number with recursion is the typical example to explain this method of programming. Remember that the factorial of a number is to multiply that number by all its previous ones until it reaches 1. It is represented with an exclamation. For example:

5! = 5x4x3x2x1 = 120 Counting numbers back  we have already done. Now what remains is to multiply that number by its previous one, and so on:

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

If you  want to simplify it further and leave your friend’s mouth wide open, do this instead:

int factorial(int n) {
   return (n == 0) ? 1: n * factorial (n-1);
}

I hope you have used the tutorial to learn something more about programming. Remember that this is done in Java, but it is valid for other languages as well.

Looking for java assignment help?
You can contact us for programming assignment help and other app development services.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top