[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Passing a function as an argument to a function

Posted on 2006-05-23
2
Medium Priority
?
199 Views
Last Modified: 2010-04-05
I have a function to find the root of a function of x:

function TMyAppForm.FindRoot(x1, x2: Double): Double;

where the root lies between x1 and x2.

At present the root finding function assumes there is an externally defined function f(x: Double): Double, the root of which is to be found. However, I would like to apply the root-finding function to several different functions of x. Hence, I am interested in passing the function as an argument. For example, in one method of the program, I may have:

function f(x: Double): Double;
begin
  f:=<some function of x>;
end;

Then in another method of the program, I may have:
function f(x: Double): Double;
begin
  f:=<some entirely different function of x>;
end;

How can I get the root finding function to operate on these different definitions of f ?
0
Comment
Question by:riskassessor
2 Comments
 
LVL 21

Accepted Solution

by:
ziolko earned 2000 total points
ID: 16749176
this should work:

type
  TMyfunction = function(x: Double):Double;

procedure PassFunc(AFunction: TMyFunction);
var d: Double;
begin
  d := AFunction(12.45);
end;

procedure Func1(x: Double):Double;
begin
  Result := x;
end;

procedure Func2(x: Double):Double;
begin
  Result := x * x;
end;

PassFunc(Func1);
PassFunc(Func2);

if Func1, Finc2 are about to be object methods
TMyFunction should be declared:
type
  TMyfunction = function(x: Double):Double of object;

ziolko.
0
 

Author Comment

by:riskassessor
ID: 16751728
Thank you very much, that seems to work! Except that it should be PassFunc(@Func1); PassFunc(@Func2), I think.

Peter
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Loops Section Overview
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question