Logo

Programming-Idioms

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

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)

uint f(in uint i) pure nothrow @nogc
in {
    assert(i <= 12);
} body {
    if (i == 0)
        return 1;
    else
        return i * f(i - 1);
}

Assertion is used to prevent silent overflow.
function F (I : Natural) return Natural is (if I < 2 then 1 else I * F (I - 1));

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