How to use procedural variables
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.
program Test;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes;
// Make the Types the type corresponds to a function signature
type
TFuncNoArgsString = function(): String;
TFuncOneArgsString = function(x: string): string;
// Example functions
function Hello: String;
begin
Result := 'Hello There';
end;
function Woah(G: String): String;
begin
Result := 'Woah ' + G;
end;
// Overloaded function takes the two types of function
// pointers created above
procedure Take(f: TFuncNoArgsString); overload;
begin
WriteLn(f());
end;
procedure Take(f: TFuncOneArgsString); overload;
begin
WriteLn(f('there!!!'));
end;
var
ptr: Pointer;
List: TList;
begin
// the "@" symbol turns the variable into a pointer.
// This must be done in order pass a function as a
// parameter. This also demonstrates that pascal
// keeps track of the pointer type so the overloading works!
Take(@Hello);
Take(@Woah);
// Now put a function in an untyped pointer
ptr := @Hello;
// Type the pointer and call it all at the same time
WriteLn(TFuncNoArgsString(ptr));
// A TList Example
List := TList.Create;
List.Add(@Hello);
WriteLn(TFuncNoArgsString(List[0]));
ReadLn;
end.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.
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.
Edited on wiki.freepascal.org by 7 people, 2006–2020
From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.
| When | Who | What changed |
|---|---|---|
| Daniel-fpc | How To Use Function Pointers moved to How to use procedural variables: Function pointers are C terminology. No IniCaps in title. (minor) | |
| Daniel-fpc | -> procvar. | |
| Chronos | ||
| Jhmos | FPC 2.4.x requires different Type for procedures | |
| Jonas | Undo revision 53902 by Special:Contributions/Jhmos (User talk:Jhmos) (a regular procedure variable and a "procedure of object" are two different things) | |
| Vincent | Text replace - "delphi>" to "syntaxhighlight>" (minor) | |
| Chronos | category | |
| Kai Burghardt | spelling; added hint about classicprocvars modeswitch | |
| Kai Burghardt | add crossreference to @ | |
| Kai Burghardt | add Category:Code | |
| Kai Burghardt | modeswitch | |
| Trev | Fixed syntax highlighting (minor) | |
| Trev | Added page template; removed categories in template (minor) | |
| Trev | Actually add page template :- (minor) |
Talk about this page
Nobody has opened a bench for this page yet. Sign in to open one.
