Factorial in Pascal
Example for versions
Free Pascal 2.0.4,
Free Pascal 2.2.0,
Turbo Pascal 4.0,
Turbo Pascal 5.0,
Turbo Pascal 5.5,
Turbo Pascal 6.0,
Turbo Pascal 7.0,
gpc 20070904
This example uses recursive factorial definition.
Note that this example works in all given implementations of Pascal, but it produces slightly different results. In gpc everything works perfectly. Turbo Pascal and Free Pascal have arithmetic overflow for factorial of numbers greater than 12, but Free Pascal reports an error:
13! = Runtime error 215 at $004013C7
$004013C7
$00401449
$004063E0
while Turbo Pascal doesn’t detect the error and simply prints wrong values:
13! = 1932053504
14! = 1278945280
15! = 2004310016
16! = 2004189184
This example doesn’t work in Turbo Pascal 3.0 and earlier due to absence of longint
data type in these versions.
In GNU Pascal this program works without any problems.
program factorial;
function fact(n: integer): longint;
begin
if (n = 0) then
fact := 1
else
fact := n * fact(n - 1);
end;
var
n: integer;
begin
for n := 0 to 16 do
writeln(n, '! = ', fact(n));
end.
Comments
]]>blog comments powered by Disqus
]]>