|
1 | | -{ Anonymous functions test. } |
2 | | - |
3 | | -{$ifdef FPC} |
4 | | - {$mode objfpc}{$H+}{$J-} |
5 | | - {$modeswitch functionreferences} |
6 | | - {$modeswitch anonymousfunctions} |
7 | | -{$endif} |
8 | | -{$apptype CONSOLE} |
9 | | - |
10 | | -type |
11 | | - TMyFunction = reference to function (const A, B: Integer): Integer; |
12 | | - |
13 | | -function ProcessTheList(const F: TMyFunction): Integer; |
14 | | -var |
15 | | - I: Integer; |
16 | | -begin |
17 | | - Result := 1; |
18 | | - for I := 2 to 10 do |
19 | | - Result := F(Result, I); |
20 | | -end; |
21 | | - |
22 | | -var |
23 | | - SomeFunction: TMyFunction; |
24 | | -begin |
25 | | - // Assign anonymous function to a variable |
26 | | - SomeFunction := |
27 | | - function(const A, B: Integer): Integer |
28 | | - begin |
29 | | - Result := A + B; |
30 | | - end; |
31 | | - WriteLn('1 + 2 + 3 ... + 10 = ', ProcessTheList(SomeFunction)); |
32 | | - |
33 | | - // Or, simpler, just define the anonymous function inside the parameter |
34 | | - WriteLn('1 + 2 + 3 ... + 10 = ', ProcessTheList( |
35 | | - function(const A, B: Integer): Integer |
36 | | - begin |
37 | | - Result := A + B; |
38 | | - end |
39 | | - )); |
40 | | - |
41 | | - // Similar test, now with multiplication |
42 | | - WriteLn('1 * 2 * 3 ... * 10 = ', ProcessTheList( |
43 | | - function(const A, B: Integer): Integer |
44 | | - begin |
45 | | - Result := A * B; |
46 | | - end |
47 | | - )); |
48 | | -end. |
| 1 | +{ Anonymous functions test. } |
| 2 | + |
| 3 | +{$ifdef FPC} |
| 4 | + {$mode objfpc}{$H+}{$J-} |
| 5 | + {$modeswitch functionreferences} |
| 6 | + {$modeswitch anonymousfunctions} |
| 7 | +{$endif} |
| 8 | +{$ifdef MSWINDOWS} {$apptype CONSOLE} {$endif} |
| 9 | + |
| 10 | +type |
| 11 | + TMyFunction = reference to function (const A, B: Integer): Integer; |
| 12 | + |
| 13 | +function ProcessTheList(const F: TMyFunction): Integer; |
| 14 | +var |
| 15 | + I: Integer; |
| 16 | +begin |
| 17 | + Result := 1; |
| 18 | + for I := 2 to 10 do |
| 19 | + Result := F(Result, I); |
| 20 | +end; |
| 21 | + |
| 22 | +var |
| 23 | + SomeFunction: TMyFunction; |
| 24 | +begin |
| 25 | + // Assign anonymous function to a variable |
| 26 | + SomeFunction := |
| 27 | + function(const A, B: Integer): Integer |
| 28 | + begin |
| 29 | + Result := A + B; |
| 30 | + end; |
| 31 | + WriteLn('1 + 2 + 3 ... + 10 = ', ProcessTheList(SomeFunction)); |
| 32 | + |
| 33 | + // Or, simpler, just define the anonymous function inside the parameter |
| 34 | + WriteLn('1 + 2 + 3 ... + 10 = ', ProcessTheList( |
| 35 | + function(const A, B: Integer): Integer |
| 36 | + begin |
| 37 | + Result := A + B; |
| 38 | + end |
| 39 | + )); |
| 40 | + |
| 41 | + // Similar test, now with multiplication |
| 42 | + WriteLn('1 * 2 * 3 ... * 10 = ', ProcessTheList( |
| 43 | + function(const A, B: Integer): Integer |
| 44 | + begin |
| 45 | + Result := A * B; |
| 46 | + end |
| 47 | + )); |
| 48 | +end. |
0 commit comments