Logo

Programming-Idioms

History of Idiom 31 > diff from v79 to v80

Edit summary for version 80 by programming-idioms.org:
[D] Function name f, argument name i

Version 79

2022-05-23, 13:20:55

Version 80

2022-05-23, 13:21:48

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
uint factorial(in uint n) pure nothrow @nogc
in {
    assert(n <= 12);
} body {
    if (n == 0)
        return 1;
    else
        return n * factorial(n - 1);
}
Code
uint f(in uint i) pure nothrow @nogc
in {
    assert(i <= 12);
} body {
    if (i == 0)
        return 1;
    else
        return i * f(i - 1);
}
Comments bubble
Assertion is used to prevent silent overflow.
Comments bubble
Assertion is used to prevent silent overflow.
Origin
http://rosettacode.org/wiki/Factorial#Recursive_Version
Origin
http://rosettacode.org/wiki/Factorial#Recursive_Version