Recursion and return keyword

I am currently working on Java tutorials and currently on recursions.

I have the following code that calculates the factorial of any number passed to the factorial method

public class App {
    public static void main(String[] args) {
        //E.g 4! = 4*3*2*1(factorial 4)
        System.out.println(factorial(4));

    }
    private static int factorial(int value){
        //System.out.println(value);
        if (value == 1){
            return 1;
        }
        return factorial(value - 1)*value;
    }
}

I find it difficult to understand part

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

I understand that the return keyword simply terminates the method and / or returns a value of the same type declared by the method (i.e. int, String, etc.).

What happens when the next line is executed?

return factorial(value - 1)*value;

the function returns the total value (value-1) * that would give me

(3)*4 = 12
(2)*3 = 6
(1)*2 = 2

with each passing iteration. However, System.out.println(factorial(4));it gives me everything 24. How is this number obtained from the method? There are no variables to store the sum of the values, so where is their program stored? Also, how do I get 24from these values?

(3)*4
(2)*3
(1)*2

, 24 4*3*2*1, , .

.

+3
8

return factorial(value - 1)*value;

, , factorial(value - 1) value. , :

return (factorial(value - 1)) * value;

, 4, :

factorial(3) * 4;

(factorial(2) * 3) * 4;

((factorial(1) * 2) * 3) * 4;

1 * 2 * 3 * 4;

, , , :

  • 4. if, , 3. ( , , , , , , . "" .)

  • if, , 2.

  • if, , 1.
  • if 1.
  • , (1) (2), (2).
  • , (2) (3), (6).
  • , (6) (4), (24).

, , 1 * 2 * 3 * 4, , .

, , :

private static int factorial(int value){
    if (value == 1){
        return 1;
    }
    int recursiveResult = factorial(value - 1);
    return recursiveResult * value;
}

, "" , , . , ( ), , . . ​​, :

int stackOverflow() { return stackOverflow(); }

, , - . , ; , stackoverflow. , :

void possibleStackOverflow(int arg) { if (arg == 0) return; possibleStackOverflow(arg - 1); }

possibleStackOverflow(10), , , , possibleStackOverflow(-1) .

, callStackOverflow (Integer.MAX_VALUE) StackOverflowException

+7

return factorial (value-1) *, (-1) .

, (4):

( (1) * ( (2 -1) * ( (3 -1) * (4 - 1)))) * 4

(1 * (2 * 3)) * 4, 24

+1
return factorial(value - 1)*value;

value - 1 value.

:

(4)

= factorial (3) * 4

= factorial (2) * 3 * 4

= factorial (1) * 2 * 3 * 4

= 1 * 2 * 3 * 4 = 24

+1

, :

factorial(4)  = 4 * factorial(4-1)
                                        |
                                3 * factorial(3-1)
                                               |
                                       2 * factorial(2-1)
                                                      |
                                                      1

.

+1

"return" /. return, "return".

, return 1 + 1; "+" . return func (); java . " " ( "", ).

, , :

1) return

2)

3)

4)...

5) " ", "1".

6)

0

. value 1, 1, value-1 , , value.

:

private static int factorial(int value){
    if (value == 1){
        return 1;
    }
    int a = factorial(value - 1);
    return a * value;
}

. a, b c, .

factorial(4)
  |-- a = factorial(4-1=3)
  |    |-- b = factorial(3-1=2)
  |    |    |-- c = factorial(2-1=1)
  |    |    |    +-- return 1          // <-- deepest point in the stack
  |    |    +-- return c * 2 = 2
  |    +-- return b * 3 = 6
  +-- return a * 4 = 24

, , factorial(value-1), 1, , . , ( !), .

0

, f(); f() f(). ; , f(). , .

0

Source: https://habr.com/ru/post/1584991/


All Articles