Sign in Join
Wiki copytemporary

How to use procedural variables: what changed

Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
1-Copy the text below and it will demonstrate the use of procedural variables, this is a fully working program. You don't even need to understand how it works. The syntax is pretty simple.
2-```
1+Copy the text below and it will demonstrate the use of procedural variables, this is a fully working program. You don't even need to understand how it works. The syntax is pretty simple.
2+
3+``` pascal
34program Test;
4-
5- {$mode objfpc}{$H+}
6- uses
7- {$IFDEF UNIX}{$IFDEF UseCThreads}
8- cthreads,
9- {$ENDIF}{$ENDIF}
10- Classes;
11-
12- // Make the Types the type corresponds to a function signature
13- type
14- TFuncNoArgsString = function(): String;
15- TFuncOneArgsString = function(x: string): string;
16-
17- // Example functions
18- function Hello: String;
19- begin
20- Result := 'Hello There';
21- end;
22-
23- function Woah(G: String): String;
24- begin
25- Result := 'Woah ' + G;
26- end;
27-
28- // Overloaded function takes the two types of function
29- // pointers created above
30- procedure Take(f: TFuncNoArgsString); overload;
31- begin
32- WriteLn(f());
33- end;
34-
35- procedure Take(f: TFuncOneArgsString); overload;
36- begin
37- WriteLn(f('there!!!'));
38- end;
39-
40- var
41- ptr: Pointer;
42- List: TList;
43- begin
44- // the "@" symbol turns the variable into a pointer.
45- // This must be done in order pass a function as a
46- // parameter. This also demonstrates that pascal
47- // keeps track of the pointer type so the overloading works!
48-
49- Take(@Hello);
50- Take(@Woah);
51-
52- // Now put a function in an untyped pointer
53- ptr := @Hello;
54- // Type the pointer and call it all at the same time
55- WriteLn(TFuncNoArgsString(ptr));
56- // A TList Example
57- List := TList.Create;
58- List.Add(@Hello);
59- WriteLn(TFuncNoArgsString(List[0]));
60- ReadLn;
61- end.
5+
6+{$mode objfpc}{$H+}
7+uses
8+ {$IFDEF UNIX}{$IFDEF UseCThreads}
9+ cthreads,
10+ {$ENDIF}{$ENDIF}
11+ Classes;
12+
13+// Make the Types the type corresponds to a function signature
14+type
15+ TFuncNoArgsString = function(): String;
16+ TFuncOneArgsString = function(x: string): string;
17+
18+// Example functions
19+function Hello: String;
20+begin
21+ Result := 'Hello There';
22+end;
23+
24+function Woah(G: String): String;
25+begin
26+ Result := 'Woah ' + G;
27+end;
28+
29+// Overloaded function takes the two types of function
30+// pointers created above
31+procedure Take(f: TFuncNoArgsString); overload;
32+begin
33+ WriteLn(f());
34+end;
35+
36+procedure Take(f: TFuncOneArgsString); overload;
37+begin
38+ WriteLn(f('there!!!'));
39+end;
40+
41+var
42+ ptr: Pointer;
43+ List: TList;
44+begin
45+ // the "@" symbol turns the variable into a pointer.
46+ // This must be done in order pass a function as a
47+ // parameter. This also demonstrates that pascal
48+ // keeps track of the pointer type so the overloading works!
49+
50+ Take(@Hello);
51+ Take(@Woah);
52+
53+ // Now put a function in an untyped pointer
54+ ptr := @Hello;
55+ // Type the pointer and call it all at the same time
56+ WriteLn(TFuncNoArgsString(ptr));
57+ // A TList Example
58+ List := TList.Create;
59+ List.Add(@Hello);
60+ WriteLn(TFuncNoArgsString(List[0]));
61+ ReadLn;
62+end.
6263```
6364
64-With `{$modeswitch classicprocvars+}` the [[@|`@`-address-operator]] is not necessary to refer to methods. Also, if you are using the `@`-address-operator usage of `{$typedaddress on}` is advised in order to prevent programming mistakes.
65+With `{$modeswitch classicprocvars+}` the [[@|@-address-operator]] is not necessary to refer to methods. Also, if you are using the `@`-address-operator usage of `{$typedaddress on}` is advised in order to prevent programming mistakes.
6566
6667---
67-*From the Free Pascal wiki, page "How to use procedural variables", as the Internet Archive kept it on 2022-07-07. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
68+*From the Free Pascal wiki, page "How to use procedural variables", as it stood on 2020-02-17 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history