ben862000
ben862000

Reputation: 41

code after recursion call

Why after recursion call the System.out.println(res) still run? I thought it will never reach the System.out.println(res);

public class recursion
{
    public static void main(String[] args)
    {
        recursion p = new recursion();
        p.perms(4, "");
    }

    public void perms(int remaining, String res)
    {
        if (remaining > 0) {
           perms(remaining - 1, res + "1");
            System.out.println(res);
        }
    }
}

Upvotes: 3

Views: 4742

Answers (5)

samtax01
samtax01

Reputation: 952

It easy to explain like this. When you perform a recursive call, The state of the function is added to a stack and it gets reversed when a base case(condition) is meant.

e.g

public class Main {

  private void printFun(int test) {
    if (test < 1) // base case
        return;
    System.out.println("Top Value : " + test);
    printFun(test - 1);
    System.out.println("Bottom Value :  " + test);
  }

  public static void main(String[] args) {
    int test = 3;
    new Main().printFun(test);
  }

}

The Output will be

Top Value : 3
Top Value : 2
Top Value : 1
Bottom Value :  1
Bottom Value :  2
Bottom Value :  3

Note that the bottom value continued from 1 since the value of the test variable when the function was called was 1 then continues to the next call while reversing the code back after the recursive line.

However, Always try to make Recursive call the last thing executed by the function. This is called Tail Recursive as it can be optimized by the compiler better than the non-tail recursive. please read more on tail-recursive on https://www.geeksforgeeks.org/tail-recursion/

Upvotes: 1

stinepike
stinepike

Reputation: 54702

think about the steps

call perms(4,"");                   //1 st rec
        perms(3,"1");               //2 nd rec
           perms(2,"11");           //3 rd rec
              perms(1,"111');       //4 th rec

then for remaining = 0 it will not go to if so now think about the reverse order

           go back to 4th rec and print 111
        go back to 3rd rec and print 11
      go back to 2nd rec and pring 1
    go back to 1st rec and print (blank space)

Upvotes: 8

anshulkatta
anshulkatta

Reputation: 2064

It will run three times Because after the fourth call , stack is popped up and it will come back to call

perms(remaining - 1, res + "1");

Then System.out.println(res) will run

For the first call it will run

CALLING ->  perms(4-1,""+"1") -> check if(3>0) ->perms(3-1,"1"+"1")-> check if(2>0) ->perms(2-1,"11"+"1") ->check if(1>0)->perms(1-1,"111"+"1")->check if(0>0)false              
STACK index ->     0                                      1                                   2                                     3               pop the stack

After 1st pop in stack , the value returns to

output of -> perms(1 - 1, res + "11"+"1");

will execute -> System.out.println(res);

same occurs for until it comes to index 0

Upvotes: 2

Bathsheba
Bathsheba

Reputation: 234785

Of course it will.

Consider, in detail, the case p.perms(1, "");

What happens is

if (1 > 0) { // this is true
    perms(0, res + "1");
    System.out.println(res);
}

and perms(0, res + "1"); is a no-op as it doesn't pass the if statement. This is your recursion block, and you've coded it sensibly. The println then executes as it is the next statement. There's nothing fundamentally different between a recursive and non-recursive function, although with recursive functions you can run out of stack space if it's called repeatedly.

You can unpick the recursion in a similar way for larger values of remaining.

Upvotes: 3

Bob Flannigon
Bob Flannigon

Reputation: 1294

Because at the end of the last recursion, when it doesn't run, all the previous recursions will then execute that line. The recursion is no different to any other method call, i.e go off and do something else, but when you're done, continue in this method at that point (well, the following line).

Upvotes: 3

Related Questions