Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Kotlin

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)

fun f(i: Int): Int = when (i) {
    0 -> 1
    else -> i * f(i - 1)
}
fun f(i: Int) = if (i == 0) 1 else i * f(i - 1)
function F (I : Natural) return Natural is (if I < 2 then 1 else I * F (I - 1));

New implementation...
< >
programming-idioms.org