-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsample006.ptuc
More file actions
48 lines (41 loc) · 753 Bytes
/
sample006.ptuc
File metadata and controls
48 lines (41 loc) · 753 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(* Passing around functions *)
program function_fun;
type
intfunc = function(n: integer) : integer;
string = array of char;
function fibonacci(n:integer) : integer;
var
f0, f1, temp: integer;
begin
f0:=0;
f1:=1;
while n>1 do
begin
temp := f1;
f1 := f1+f0;
f0 := temp;
n := n-1
end;
result := f1
end;
function factorial(n:integer) : integer;
var
fac, i: integer;
begin
fac := 1;
for i:=n downto 1 do
fac:=i*fac;
result := fac;
return
end;
procedure eval(prompt: string; f: intfunc; val: integer);
begin
writeString(prompt);
writeString('('); writeInteger(val); writeString(')=');
writeInteger(f(val));
writeString('\n')
end;
begin
eval('Fibonacci', fibonacci, 5);
eval('factorial', factorial, 5)
end.