Technical Information Database TI447D.txt - Using procedural varialbles in Pascal Category :Object Pascal Platform :All-32Bit Product : Description: The included program, FUNC1.PAS shows how to use procedural variables. It creates both a pointer to a function and a pointer to a procedure and shows how to use them. Func1 creates a pointer to a function called MyFunc, calls it once and then passes it to a procedure which then proceeds to call it. Therefore this examples shows how to assign and call a procedural variable, and also how to pass it to another procedure which can then call it. The following lines demonstrate how to create types that can point at a procedure or function: type TMyFunc = function(i: Integer): Integer; TMyProc = procedure; Notice that the actual name of the function or procedure is left out, but that it is necessary to declare any variables passed to function or procedure, and to specify any return types from a function. Its important to understand that TMyFunc and TMyProc are really pointers to functions, even though there is no need to call new and dispose, and no need to dereference the pointer with the caret symbol ("^"). Notice also that both MyProcedure and MyFunc are declared "Far". This is absolutely necessary when working with procedural variables, though of course functions declared in the header to a unit will automatically be far when called from another module. program Func1; type TMyFunc = function(i: Integer): Integer; TMyProc = procedure; procedure MyProcedure; far; begin WriteLn('Hi'); end; function MyFunc(I: Integer): Integer; far; begin WriteLn(i); end; procedure Foo(F: TMyFunc); begin F(5); end; var F: TMyFunc; P: TMyProc; begin F := MyFunc; F(10); Foo(F); P := MyProcedure; P; end. Reference: 4/22/99 1:02:30 PM
Last Modified: 01-SEP-99