AirZone
AirZone

Reputation: 11

Recursive in Java when there is a return statement after the method called itself

Code:

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

public static int test(int a){
    if(a<10)
        return a;
    int b = (a%10);
    int c = test(a/10);
    int d = Math.max(b,c);
    return d;
}

I understand what the method does (after using the debugger) and I understand that the method calls itself till it is less than 10 and it runs and checks what's bigger, b or c. Now what I don't understand is that why when there is the return statement return d; it returns to int c = test(a/10) and not to the start of the method of int test(int a){.

Upvotes: 1

Views: 1530

Answers (5)

GameDroids
GameDroids

Reputation: 5662

When you call test(13549) this is what happens:

test(13549){
   false
   b=9
   c= test(1354){
       false
       b=4
       c=test(135){
           false
           b=5
           c=test(13){
               false
               b=3
               c=test(1){
                  true
                  return 1
                }
                d=Max(3,1)
                return 3
              }
               d=Max(3,3)
               return 3
             }
             d=Max(5,3)
             return 5
           }
          d=Max(4,5)
          return 5
        }
    d=Max(9,5)
    return 9
  }

Sorry if I may have miscalculated in any way.. but there you can see, that the first return statement is not reached until all recursive calls are finished and the very first c hast an actual value.

Upvotes: 0

LucienK
LucienK

Reputation: 310

This is a basic idea of recursion - when you call the method within itself, it isn't just jumping back to the start of the method.

visually:

1
|
|  2
|__|
|  |  3
|  |__|
   |  |  x
   |  |__|
      |  .
      |  .

Where each vertical line the running of the function, and the horizontal lines are recursive calls.

After each call, it returns back to the place in the instance of the method it was called in Effectively, that means that in the 3rd recursion, you have 3 versions of the function on the stack (in memory), rather than simply going back to the top of the current function 3 times.

Upvotes: 1

j.jerrod.taylor
j.jerrod.taylor

Reputation: 1140

Each time you call your recursive function, it starts from the top of the function. Since you have d defined after your recursive function call you will never get to d. You will continue to recursively call your function until you are able to return a.

Upvotes: 0

gerrytan
gerrytan

Reputation: 41153

When tracing your code, imagine you have a stack (of function execution). Whenever you find the function is calling itself, put a new function execution on top of the stack, and when the function returns take out the stack element.

You'll find that the function will keep calling itself until it reach the 'base case' -- when a<10.

Putting a code below a return statement won't do any good (in some language it won't even compile), none of the code will get executed once the function returns

Upvotes: 0

Louis Wasserman
Louis Wasserman

Reputation: 198571

The return statement returns the output of the call to test. So on the line return d; it's just returning the value of test(a/10) in the line c = test(a/10).

Upvotes: 3

Related Questions