Why does the function not work without the "if" statement?

Ahmed Twba

This function returns a list containing all numbers from n to 0.
If I deleted the "if" statement the function doesn't work, even if the parameters aren't 0.
Why is that?

fun countDown (n : Int): List<Int> {
    if (n == 0) {
        return listOf(0)
    }

    return mutableListOf(n).also { it.addAll(countDown(n - 1)) }
}
ralphmerridew

Your code calls it.addAll(countDown(n-1)), specifically calling countDown(n-1)

So a direct call to, say, countDown(3) will call countDown(2); countDown(2) calls countDown(1); countDown(1) calls countDown(0).

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 does this function work with min, but not without?

From Dev

Why does == work and = does not in an if statement?

From Dev

Why does inserting a printf statement make my function work correctly?

From Java

Why does a main function without a return statement return value 12?

From Dev

Calling a javascript function in JSX: why does calling a function without the () work?

From Dev

Why does such statement work in MySQL?

From Dev

Why the second statement does not work?

From Dev

Why does this if statement not work as expected?

From Dev

Why this lock statement does not work

From Dev

Why does single `=` work in `if` statement?

From Dev

javascript - Why does this if statement not work?

From Dev

Why does this case statement not work?

From Dev

Why does this statement work in Java?

From Dev

Why If statement does not work with tkinter

From Dev

Why/How does this extension function work? How to call it without `with`?

From Dev

Why does this pointer to function work without warnings or errors?

From Dev

PostgreSQL group by without aggregate function. Why does it work?

From Dev

Why does "this" work differently when function is called with and without "new"

From Dev

When creating a new column, why does it work with a function but not without it?

From Dev

ReactJs - Why does calling onDelete() with parantheses without an arrow function not work?

From Dev

Why in this IF function, does Asterix (*) work but the AND function does not?

From Dev

Why does this rand use statement work in rust?

From Java

Why does this break statement break not work?

From Dev

Why does this statement only work with a WHERE?

From Dev

Why does my JavaScript switch statement not work?

From Dev

Why does this only work with a return statement here?

From Dev

Why does my IF statement not work on python?

From Dev

Why does the break statement not work here?

From Dev

Why does my "INSERT INTO" Statement not work?

Related Related

  1. 1

    Why does this function work with min, but not without?

  2. 2

    Why does == work and = does not in an if statement?

  3. 3

    Why does inserting a printf statement make my function work correctly?

  4. 4

    Why does a main function without a return statement return value 12?

  5. 5

    Calling a javascript function in JSX: why does calling a function without the () work?

  6. 6

    Why does such statement work in MySQL?

  7. 7

    Why the second statement does not work?

  8. 8

    Why does this if statement not work as expected?

  9. 9

    Why this lock statement does not work

  10. 10

    Why does single `=` work in `if` statement?

  11. 11

    javascript - Why does this if statement not work?

  12. 12

    Why does this case statement not work?

  13. 13

    Why does this statement work in Java?

  14. 14

    Why If statement does not work with tkinter

  15. 15

    Why/How does this extension function work? How to call it without `with`?

  16. 16

    Why does this pointer to function work without warnings or errors?

  17. 17

    PostgreSQL group by without aggregate function. Why does it work?

  18. 18

    Why does "this" work differently when function is called with and without "new"

  19. 19

    When creating a new column, why does it work with a function but not without it?

  20. 20

    ReactJs - Why does calling onDelete() with parantheses without an arrow function not work?

  21. 21

    Why in this IF function, does Asterix (*) work but the AND function does not?

  22. 22

    Why does this rand use statement work in rust?

  23. 23

    Why does this break statement break not work?

  24. 24

    Why does this statement only work with a WHERE?

  25. 25

    Why does my JavaScript switch statement not work?

  26. 26

    Why does this only work with a return statement here?

  27. 27

    Why does my IF statement not work on python?

  28. 28

    Why does the break statement not work here?

  29. 29

    Why does my "INSERT INTO" Statement not work?

HotTag

Archive