Function pointers in Delphi

sudhakar_koundinya
sudhakar_koundinya used Ask the Experts™
on
Hi all

How to create and use function pointers in delphi. Some thing like information provided for c++/c in the following link http://www.newty.de/CCPP/FPT/em_fpt.html#syntax
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2004
Commented:
? maybe something like this

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

type TFunc = function(AValue1,AValue2 : Extended) : Extended;

var
  Form1: TForm1;

implementation

{$R *.dfm}

//now here is such a TFunc
Function Multiply(AValue1,AValue2 : Extended) : Extended;
begin
  result := AValue1*AValue2;
end;

Function Add(AValue1,AValue2 : Extended) : Extended;
begin
  result := AValue1+AValue2;
end;

//a Function Caller
Function DoCallFunction(AFunc : TFunc;AValue1,AValue2 : Extended) : Extended;
begin
  result := AFunc(AValue1,AValue2);
end;

//a usage sample
procedure TForm1.Button1Click(Sender: TObject);
begin
  showmessage(FloatToStr(DoCallFunction(Multiply,8,9)));
  showmessage(FloatToStr(DoCallFunction(Add,8,9)));
end;

end.

meikl ;-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial