-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.barrett
executable file
·56 lines (46 loc) · 1.11 KB
/
functions.barrett
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
48
49
50
51
52
53
54
55
56
var
X, Y;
function ExecuteFunction(f, param);
begin
f(param);
end;
function Print(a);
begin
WriteLn(a);
end;
function AddFive(x);
begin
WriteLn(x + 5);
end;
function GetFunction(num);
function PrintHello;
begin
WriteLn("Hello");
end;
function PrintGoodbye;
begin
WriteLn("Goodbye");
end;
begin
if num = 1 then
begin
Result := @PrintHello;
end
else
begin
Result := @PrintGoodbye;
end;
end;
begin
{ Pass a function to another function }
{ '@' is placed in front of function name to pass without evaluating }
ExecuteFunction(@Print, "Hello World");
ExecuteFunction(@AddFive, 3);
{ Functions can be returned as well }
X := 1;
Y := GetFunction(X); { Should return a function that prints "Hello" }
Y; { Executes the function returned by GetFunction }
X := 2;
Y := GetFunction(X); { Should return a function that prints "Goodbye" }
Y; { Executes the function returned by GetFunction }
end;