Logo

Programming-Idioms

History of Idiom 31 > diff from v72 to v73

Edit summary for version 73 by AlfRichter:
[Kotlin] Add Demo

Version 72

2021-09-24, 12:17:50

Version 73

2022-02-01, 23:40:02

Idiom #31 Recursive factorial (simple)

Create the recursive function f which returns the factorial of the non-negative integer i, calculated from f(i-1)

Idiom #31 Recursive factorial (simple)

Create the recursive function f which returns the factorial of the non-negative integer i, calculated from f(i-1)

Variables
f,i
Variables
f,i
Code
fun f(i: Int): Int = when (i) {
    0 -> 1
    else -> i * f(i - 1)
}
Code
fun f(i: Int): Int = when (i) {
    0 -> 1
    else -> i * f(i - 1)
}
Doc URL
https://kotlinlang.org/docs/functions.html#tail-recursive-functions
Demo URL
https://pl.kotl.in/XubKSuIwN