How to use procedural variables: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 2 | - | ||
| 3 | - | ||
| 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 | + | ``` | |
| 4 | 3 | program Test; | |
| 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 | - | ||
| 57 | - | ||
| 58 | - | ||
| 59 | - | ||
| 60 | - | ||
| 61 | - | ||
| 62 | - | ||
| 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. | |
| 63 | 62 | ``` | |
| 64 | 63 | ||
| 65 | - | ||
| 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. | |
| 66 | 65 | ||
| 67 | 66 | --- | |
| 68 | - | ||
| 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).* |
