Skip to main content
corected text to match the code
Source Link

The key here is stack frames. Lets take a look at your first example:

public static int incrementI(int i) {
    if (i == 5){
        return i;           
    } else {
         incrementI(i + 1);
    }
    return i;  
}

On the first call, you have a variable named i, with the value of 0. Since i isn't 05, its going to call incrementI again with 1.

This time around, you have a new stack frame, and a different variable named i. Every time you call incrementI, a brand new i is being created with the new value.

When you've called incrementI 6 times, each function call has its own copy of i:

 incrementI: 5
 incrementI: 4
 incrementI: 3
 incrementI: 2
 incrementI: 1
 incrementI: 0

The top function returns 5 (as that is i), and then the next function returns 4 (as that is its copy of i), until the bottom function returns 0.

In your second example, the same thing occurs, except each function is returning what the above function returned. Therefore, the top function returns 5, then the next function returns 5 (as that is what the previous function returned), and so on.

The key here is stack frames. Lets take a look at your first example:

public static int incrementI(int i) {
    if (i == 5){
        return i;           
    } else {
         incrementI(i + 1);
    }
    return i;  
}

On the first call, you have a variable named i, with the value of 0. Since i isn't 0, its going to call incrementI again with 1.

This time around, you have a new stack frame, and a different variable named i. Every time you call incrementI, a brand new i is being created with the new value.

When you've called incrementI 6 times, each function call has its own copy of i:

 incrementI: 5
 incrementI: 4
 incrementI: 3
 incrementI: 2
 incrementI: 1
 incrementI: 0

The top function returns 5 (as that is i), and then the next function returns 4 (as that is its copy of i), until the bottom function returns 0.

In your second example, the same thing occurs, except each function is returning what the above function returned. Therefore, the top function returns 5, then the next function returns 5 (as that is what the previous function returned), and so on.

The key here is stack frames. Lets take a look at your first example:

public static int incrementI(int i) {
    if (i == 5){
        return i;           
    } else {
         incrementI(i + 1);
    }
    return i;  
}

On the first call, you have a variable named i, with the value of 0. Since i isn't 5, its going to call incrementI again with 1.

This time around, you have a new stack frame, and a different variable named i. Every time you call incrementI, a brand new i is being created with the new value.

When you've called incrementI 6 times, each function call has its own copy of i:

 incrementI: 5
 incrementI: 4
 incrementI: 3
 incrementI: 2
 incrementI: 1
 incrementI: 0

The top function returns 5 (as that is i), and then the next function returns 4 (as that is its copy of i), until the bottom function returns 0.

In your second example, the same thing occurs, except each function is returning what the above function returned. Therefore, the top function returns 5, then the next function returns 5 (as that is what the previous function returned), and so on.

Source Link
Nathan Merrill
  • 2.4k
  • 2
  • 17
  • 20

The key here is stack frames. Lets take a look at your first example:

public static int incrementI(int i) {
    if (i == 5){
        return i;           
    } else {
         incrementI(i + 1);
    }
    return i;  
}

On the first call, you have a variable named i, with the value of 0. Since i isn't 0, its going to call incrementI again with 1.

This time around, you have a new stack frame, and a different variable named i. Every time you call incrementI, a brand new i is being created with the new value.

When you've called incrementI 6 times, each function call has its own copy of i:

 incrementI: 5
 incrementI: 4
 incrementI: 3
 incrementI: 2
 incrementI: 1
 incrementI: 0

The top function returns 5 (as that is i), and then the next function returns 4 (as that is its copy of i), until the bottom function returns 0.

In your second example, the same thing occurs, except each function is returning what the above function returned. Therefore, the top function returns 5, then the next function returns 5 (as that is what the previous function returned), and so on.