Recursion

suggest change

Looping via recursion is also possible in Kotlin as in most programming languages.

fun factorial(n: Long): Long = if (n == 0) 1 else n * factorial(n - 1)

println(factorial(10)) // 3628800

In the example above, factorial calls itself until n is 0. A negative n recurses until the stack overflows.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents