简体   繁体   中英

How does the return keyword really work?

A simple code snippet calculating the factorial of a number below:

public class Main{

static int factorial(int number){       

    System.out.println("At factorial("+number+")");

    if (number == 1){
        return 1;
    }
    return number * factorial(number-1);
}

public static void main(String[] args){

    System.out.println("factorial(4) = "+ factorial(4));


}
}

Here, I thought that when the value of the number becomes 1(due to recursion), it returns 1 every time and breaks out of the method and prints 1. But, it does return proper factorial value? As per this: Java - Does returning a value break a loop? , shouldn't it have returned 1 every time? How these 2 code snippet differ in returning a value?

Thank You

I have tried to explain the whole process; refer to this source .

Prior to this you should have basic understanding of call stacks, recursion, and stack frames.

A call stack is a data structure used by the program to store information about the active subroutines. The main reason for having a call stack is so that the program can keep track of where a subroutine should return control to once it finishes executing.

A stack frame is a part of the call stack, and a new stack frame is created every time a subroutine is called. So, in our recursive factorial() method above, a new stack frame is created every time the method is called. The stack frame is used to store all of the variables for one invocation of a routine. So, remember that a call stack is basically a stack of stack frames.


Let's divide the process into 4 parts, since the number chosen is 4.

1st Iteration

static int factorial(4){ 

    if (4 == 1){
        return 1;
    }
    return 4 * factorial(3);
}

2nd Iteration

static int factorial(3){ 

    if (3 == 1){
        return 1;
    }
    return 3 * factorial(2);
}

3rd Iteration

static int factorial(2){ 

    if (2 == 1){
        return 1;
    }
    return 2 * factorial(1);
}

4th Iteration

static int factorial(1){ 

    if (1 == 1){
        return 1;
    }
    return 2 * factorial(number-1);
}
  1. You can see that the first stack frame is created with a number equal to 4.

  2. Then a call to factorial(3) is made – so the first call to factorial(4) does not run to completion because another call ( factorial(3) ) is made before the very first call to factorial can run to completion.

  3. Same is the case for others.

  4. A stack frame is used to hold the state of the first call to factorial() . It stores the local function variables (and their values) of the current invocation of factorial() , and it will also store the return address of the method that called it (since we are talking about the very first non-recursive invocation of factorial() , whatever routine invoked factorial() in the first place is where Factorial would return when it is completely done with everything ).

Because the stack frame also stores the return address, the factorial() function knows where to return to when it finishes running

Process Execution

Finally, in the 4th stack frame, we run into our base case, which means the recursive calls are finished and then control is returned to the 3rd stack frame, where factorial(1) * 2 is calculated to be 2 , and then control is returned to the 2nd stack frame where factorial(2) * 3 is calculated to be 6 , and then control is returned to the 1st stack frame where factorial(3) * 4 is calculated to be 24 . Finally, our result of 24 is returned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM