Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to store a procedure or function in a variable Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
30-Aug-02
Category
Algorithm
Language
Delphi All Versions
Views
115
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

Is there some way I can store a procedure or function in a variable so I can call 
the procedure with the variable? I'm thinking of something similar to where you can 
declare a variable of a certain object type, then assign different objects of that 
type to the variable. If it can be done with procedures, how would it be assigned 
and what would the syntax be to call the procedure?

Answer:

Yes, you can declare a procedural type for functions with the same parameter list 
and function type. Briefly it looks something like this:


1   { ... }
2   type
3     TMathFunc = function(A, B: double): double; {defines signature of function}
4     { ... }
5   var
6     mathfunc: TMathFunc;
7     answer: double;
8     { ... }
9   
10  {Now if you define two functions}
11  
12  function Adder(A, B: double): double;
13  begin
14    result := A + B;
15  end;
16  
17  function Multiplier(A, B: double): double;
18  begin
19    result := A * B;
20  end;
21  
22  begin
23    {You can do this}
24    mathfunc := Adder;
25    answer := mathfunc(5, 9);
26    mathfunc := Multiplier;
27    answer := mathfunc(5, 9);
28  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC