Why this function return is not returning expected results in Java?

jiya singh

Below is a function which takes int and return int. Within the function I call the function itself and if i==1, I want to go out of my function.

Basically I'm trying to calculate factorial in recursive manner.

Code Snippet

static int factorial(int i){
   result = result * i;
   if(i==1){
       return 0;
   }
   factorial(i-1);
   return 1;
}

Note - result is an global int variable and is initialize to 1.

Why this function returns 1 instead of 0. [Note when i==1 then with return statement the pointer should come out of the function]

Please don't post better algorithm for factorial, I'm looking for - why this code is behaving a bit different.

user14971157

example calling factorial(3):

int i = factorial(3);
    +--------------------------------------------------+
    |result = result * 3;                              |
    |if (i==1) {                                       |
    |  // not executed                                 |
    |}                                                 |
    |factorial(2);                                     |
    |    +-------------------------------------------+ |
    |    |result = result * 2;                       | |
    |    |if (i==1) {                                | |
    |    |  // not executed                          | |
    |    |}                                          | |
    |    |factorial(1);                              | |
    |    |    +---------------------------------+    | |
    |    |    |result = result * 1;             |    | |
    |    |    |if (i==1) {                      |    | |
    |    |    |  return 0;                      |    | |
    |    |    |// nothing more in factorial(1)  |    | |
    |    |    +---------------------------------+    | |
    |    |// factorial(1) returned 0 (value not used)| |
    |    |return 1;  // factorial(2)                 | |
    |    +-------------------------------------------+ |
    |// factorial(2) returned 1 (value not used)       |
    |return 1;  // factorial(3)                        |
    +--------------------------------------------------+
i = 1; // the value returned by last call

factorial(3) returns 1! recursion was terminated by return 0, but that value is not being used

Zero would have been returned if the last two lines were joined to return factorial(i-1);


only factorial(1) will return 0, e.g. int i = factorial(1)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why this function return is not returning expected results in Java?

From Dev

why this function is not returning value expected

From Dev

Excel SEARCH function not returning expected results

From Dev

Why does not this DL query return expected results?

From Dev

Subquery not returning expected results

From Dev

ElasticSearch not returning expected results

From Dev

Percolate not returning results as expected

From Dev

Subquery not returning expected results

From Dev

Percolate not returning results as expected

From Dev

Mysql not returning expected results

From Java

Search function stored procedure with multiple fields not returning expected results

From Dev

php Scan function not returning results as expected after moving script path:

From Dev

Return statement not returning as expected

From Dev

Return non-returning function from Promise results in Warning

From Java

Left Join Not Returning Expected Results

From Dev

JPQL query not returning expected results

From Dev

foreach loop not returning expected results

From Dev

SQL injection not returning expected results

From Dev

SQL NOT EXISTS not returning expected results

From Dev

Query not returning expected results group by

From Java

Function not returning the expected result

From Dev

R Shiny rows_all to return filtered rows not returning expected results

From Dev

Java Calendar. After function is not returning expected result

From Dev

returning results to outer function

From Dev

Python returning function results

From Dev

Why is this Java query failing? returning 0 when there are results

From Dev

Why is my function returning garbage when it should return a char?

From Dev

Why is my bash function returning an unexpected return code?

From Dev

Why is my function returning garbage when it should return a char?

Related Related

  1. 1

    Why this function return is not returning expected results in Java?

  2. 2

    why this function is not returning value expected

  3. 3

    Excel SEARCH function not returning expected results

  4. 4

    Why does not this DL query return expected results?

  5. 5

    Subquery not returning expected results

  6. 6

    ElasticSearch not returning expected results

  7. 7

    Percolate not returning results as expected

  8. 8

    Subquery not returning expected results

  9. 9

    Percolate not returning results as expected

  10. 10

    Mysql not returning expected results

  11. 11

    Search function stored procedure with multiple fields not returning expected results

  12. 12

    php Scan function not returning results as expected after moving script path:

  13. 13

    Return statement not returning as expected

  14. 14

    Return non-returning function from Promise results in Warning

  15. 15

    Left Join Not Returning Expected Results

  16. 16

    JPQL query not returning expected results

  17. 17

    foreach loop not returning expected results

  18. 18

    SQL injection not returning expected results

  19. 19

    SQL NOT EXISTS not returning expected results

  20. 20

    Query not returning expected results group by

  21. 21

    Function not returning the expected result

  22. 22

    R Shiny rows_all to return filtered rows not returning expected results

  23. 23

    Java Calendar. After function is not returning expected result

  24. 24

    returning results to outer function

  25. 25

    Python returning function results

  26. 26

    Why is this Java query failing? returning 0 when there are results

  27. 27

    Why is my function returning garbage when it should return a char?

  28. 28

    Why is my bash function returning an unexpected return code?

  29. 29

    Why is my function returning garbage when it should return a char?

HotTag

Archive