[fpc-pascal] Calling a function by name (as string) read from a text file
Jilani Khaldi
jilani at cheapnet.it
Wed Oct 10 12:21:04 CEST 2007
Hi All,
I have this program:
program mainfun;
{$mode objfpc}
uses
SysUtils;
type
SetupFun = function(const vect: array of double): double;
MyRec = record
x: array of double;
myFunName: ShortString; // function name to evoke
end;
function SetMeUp(const x1: array of double): double;
var
i: integer;
sum: double;
begin
sum := 0.0;
for i := 0 to High(x1) do
sum := sum + x1[i];
result := sum;
end;
// main
var
MR: MyRec;
y: double;
st: string;
f: text;
i, count: integer;
evokeFun: SetupFun;
begin
// read MR from a text file...
assign(f, 'data.txt');
{$I-}
reset(f);
{$I+}
readln(f, MR.myFunName);
readln(f, count);
setlength(MR.x,count+1);
for i := 0 to count do
readln(f, MR.x[i]);
closefile(f);
writeln(MR.myFunName);
writeln(High(MR.x));
for i := 0 to High(MR.x) do
writeln(MR.x[i]);
{////////////////////////////////////////////////
Is there a way to "connect" the name of the function as a string "evoke"
to the true function "evokeFun: SetupFun;" getting a new function
"newEvokeFun: SetupFun;" so I can write:
////////////////////////////////////////////////}
newEvokeFun := @SetMeUp;
y := newEvokeFun(MR.x);
st := Format('Value: %6.5f', [y]);
writeln(st);
setlength(MR.x,0);
readln;
end.
{
"data.txt" file contains:
evokeFun
1
3.141590000000000E+000
2.718280000000000E+000
}
I hope I have been clear in my request.
Thank you very much.
Jilani
More information about the fpc-pascal
mailing list