Quick start manual

5-30
Delphi Language Guide
Procedural types
Procedural types
Procedural types allow you to treat procedures and functions as values that can be
assigned to variables or passed to other procedures and functions. For example,
suppose you define a function called Calc that takes two integer parameters and
returns an integer:
function Calc(X,Y: Integer): Integer;
You can assign the Calc function to the variable F:
var F: function(X,Y: Integer): Integer;
F := Calc;
If you take any procedure or function heading and remove the identifier after the
word procedure or function, what’s left is the name of a procedural type. You can
use such type names directly in variable declarations (as in the previous example) or
to declare new types:
type
TIntegerFunction = function: Integer;
TProcedure = procedure;
TStrProc = procedure(const S: string);
TMathFunc = function(X: Double): Double;
var
F: TIntegerFunction; { F is a parameterless function that returns an integer }
Proc: TProcedure; { Proc is a parameterless procedure }
SP: TStrProc; { SP is a procedure that takes a string parameter }
M: TMathFunc; { M is a function that takes a Double (real) parameter
and returns a Double }
procedure FuncProc(P: TIntegerFunction); { FuncProc is a procedure whose only parameter
is a parameterless integer-valued function }
PShortString ShortString. Useful when porting legacy code that uses the old PString
type.
PTextBuf TTextBuf (declared in SysUtils). TTextBuf is the internal buffer type in a
TTextRec file record.)
PVarRec TVarRec (declared in System)
PVariant Variant
PWideString WideString
PWordArray TWordArray (declared in SysUtils). Used to typecast dynamically
allocated memory for arrays of 2-byte values.
Table 5.6 Selected pointer types declared in System and SysUtils (continued)
Pointer type Points to variables of type