Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursion - why use return statement

Tags:

java

recursion

I am learning recursion and below is one example that I am tracing through to better understand it

public static void main(String []args) {
    new TestRecursion().strRecur("abc");
}

public void strRecur(String s) {
    if(s.length() < 6) {
        System.out.println(s);
        strRecur(s+"*");
    }
}

Below is what I have understood so far. - In the first call to strRecur("abc"), the method gets added to execution stack. It prints "abc" before pausing due to a recursive call with parameter "abc*".

  • The second call with "abc*", pushes method strRecur(abc*) to the stack and prints "abc*" to console.

  • The third call with "abc**", pushes method strRecur(abc**) to the stack and prints "abc**" to console.

  • The fourth call with "abc***", pushes method strRecur(abc***) to the stack. Since the base condition is met, the method completes (without printing anything) and pops out of the stack.

  • The third call with "abc**" is completed and popped out. Since there is no pending code to execute nothing happens.

  • The second call with "abc*" is completed and popped out. Since there is no pending code to execute nothing happens.

  • The initial call with "abc" is completed and popped out. Since there is no pending code to execute nothing happens.

Stack

Prints following to the console -

abc
abc*
abc**

I think I understand what is happening here. Now, I want to try a slight variation of this code. Instead of callingstrRecur(s+"*"), I would like to do return strRecur(s+"*")

public class TestRecursion {

    public static void main(String []args) {
        new TestRecursion().strRecur("abc");
    }

    public String strRecur(String s) {
        if(s.length() < 6) {
            System.out.println(s);
            return strRecur(s+"*");
        }
        return "Outside If";
    }
}

My expectation was that, once strRecur(abc***) pops out, it's output (abc***) would be returned to the next strRecur() on the stack, so I would see abc**** printed in the console. Same for other recursive calls as well.

However, the output in this case is exactly the same as when there is no return statement. My understanding (which of course is not correct) stems from the recursive factorial implementation, where we do something like

return n * fact(n-1);

Here fact(n-1) is resolved with the returned value of n, once the previous method on the stack completes. Why doesn't return behave in the same way in this example?

like image 735
swati Avatar asked Jul 13 '17 09:07

swati


1 Answers

The output of both methods is produced by the println statements within the recursive method, which is why it is identical in both methods.

The value returned by the second method is not printed anywhere, which is why you don't see it displayed.

If you would print the result of the second method, you will see it is "Outside If", since that's the output of the last recursive call, which is then returned by all the other method calls.

If you want the output to be abc***, you should

return s;

instead of

return "Outside If";

The full code would be:

public static void main(String[] args) {
    System.out.println(new TestRecursion().strRecur("abc"));
}

public String strRecur(String s) {
    if(s.length() < 6) {
        return strRecur(s+"*");
    }
    return s;
}
like image 67
Eran Avatar answered Sep 20 '22 11:09

Eran