4

I have a problem with numbers that I want to increment using a recursive function. The function didn't return the value as I was expecting. Upon debugging I saw the numbers being increased but when the value is returned, it appears that the adjustments to the numbers are returned in reverse order.

Here is a simple example:

private static int recursionTest(int num){
    if (num < 10){
        recursionTest(++num);
    }
    return num;
}

public static void main(String[] args) {
    System.out.println(recursionTest(1));
}

The output is always 2. When I'm using the step-through debugger I see the following:

  1. The number increases by 1 with each iteration.
  2. When the number reaches 10 the return statement is executed.
  3. The debugger then highlights the "recursionTest(++num);" line but the number is decreased by 1.
  4. The return statement is executed again.
  5. Steps 3 and 4 is repeated until a value of 2 is finally returned.

Why is the value being decremented in the end, and how do I return the initially calculated value?

6 Answers 6

12

It should be like this:

private static int recursionTest(int num){
    if (num < 10){
       return recursionTest(++num);
    }
    else
       return num;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, I didn't realise I had to use the return statement on the method-call as well.
@Rafa Can you explain why we need to return? I couldn't understand it. What's the difference between calling the method with and without return
@Octopus without return, the method will execute independently in which, the final value of num is not returned for the method. return recursionTest(++num) can also be replaced with num = recursionTest(++num). But at the end, I believe that you do understand it, maybe even better than I do :p
Thank you, Man. I can understand now. I learnt a new thing today ;)
you're welcome. glad to 'help' you understand "new thing" today :p
2

This is because you're still returning num instead of the result of your recursive function.

private static int recursionTest(int num){
    if (num < 10){
        return recursionTest(++num); 
    }
    return num;
}

Comments

2

Try using this:

num = recursionTest(++num);

1 Comment

Also a valid answer, just not as clean as the two above.
1

Your problem is that you must return the recursive calling, not the number. If you do so you are going to obtain always your initial value + 1.

Solution:

private static int recursionTest(int num){
    if(num < 10){
        return recursionTest(num + 1);
    }

    return num;
}

Comments

0

Try this, use end condition explicitly:

int recursionTest(int num) {
        if(num == 10){ //recursion exit condition
            return num;
        }
        return recursionTest(++num);
    }

Comments

-2
private static int recursionTest(int num){
   while(num < 10){
        recursionTest(++num);
        if(num < 10){
            break;
        }
    }
    return num;
}

Above snippet will iterate while loop 10 times only so it will be fast.

3 Comments

Then this is while loop, not recursion. And, you already specify while(num < 10) so no need to check if(num < 10) (which I assume what you mean is if(num >= 10))
It is not necessary to write inner IF condition, without it will also work but in that scenario if you add print statement before and after recursionTest function you will find out that it will print approx 1000 lines so I put this inner IF check so that it will print only 10 lines means when num<10 it will break the loop and will not execute further. so performance will imporve
oh okay I got it. but this is recursion, we don't need while loop because the recursive method itself will do the loop. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.