An Anonymous Method Parameter

As a more interesting example (with even more surprising syntax), we can pass an anonymous method as parameter to a function. Suppose you have a function taking an anonymous method parameter:

procedure CallTwice (value: Integer;

anlntProc: TIntProc); begi n anlntProc (value); Inc (value); anlntProc (value); end ;

The function calls the method passed as parameter twice with two consecutive integers values, the one passed as parameter and the following one. You call the function by passing an actual anonymous method to it, with directly in-place code that looks surprising:

procedure TFormAnonymFirst.btnProcParamClick(

Sender: TObject); begi n

CallTwice (48, procedure (n: Integer) begi n

  1. Lines.Add (IntToHex (n, 4)); end); CallTwice (100, procedure (n: Integer) begi n
  2. Lines.Add (FloatToStr(Sqrt(n))); end);

From the syntax point of view notice the procedure passed as parameter withing parentheses and not terminated by a semicolon. The actual effect of the code is to call the IntToHex with 48 and 49 and the FloatToStr on the square root of 100 and 101, producing the following output:


0031 10


Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment