Logo

Programming-Idioms

History of Idiom 31 > diff from v16 to v17

Edit summary for version 17 by :

Version 16

2015-09-05, 19:00:54

Version 17

2015-09-09, 09:01:18

Idiom #31 Recursive factorial (simple)

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

Idiom #31 Recursive factorial (simple)

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

Code
type
  TPositiveInt = 0..MaxInt;

function _f(_i: TPositiveInt): Integer;
begin
  if (_i < 2) then
    Result := 1
  else
    Result := _f(_i - 1);
end; 
Code
type
  TPositiveInt = 0..MaxInt;

function _f(_i: TPositiveInt): Integer;
begin
  if (_i < 2) then
    Result := 1
  else
    Result := _f(_i - 1);
end; 
Comments bubble
The definition of type TPositiveInt allows for rangechecking on the input.
MaxInt is already defined in the language.
Comments bubble
The definition of type TPositiveInt allows for rangechecking on the input.
MaxInt is already defined in the language.