PDA

View Full Version : Function as parameter?



edfromhumanresources
04-22-2011, 04:50 PM
In C you can use a pointer to a function as a parameter in another function like so:


void MyFunction( void (*ParameterFunction)(ParameterFunctionsParameters) ) {
}


Is this possible in pascal?

Echo_
04-22-2011, 05:21 PM
I'm pretty sure you can do it in pascal considering you can do it in Simba scripts. Here is a commonly used example:

MMouse(x, y, RandomRange(-2, 2), RandomRange(-2, 2));

So it will use the return value of the RandomRange function for the pixel randomness of x and y. You just have to make sure that the function you are using as the parameter returns the data type needed.

Sex
04-22-2011, 06:00 PM
function TestFunc : string;
begin
result := 'test';
end;

procedure Test(Func : function : string);
begin
Writeln(Func());
end;

begin
Test(@TestFunc);
end. or
function TestFunc : string;
begin
result := 'test';
end;

procedure Test(Func : string; Params : TVariantArray);
begin
writeln(CallProc('TestFunc', Params));
end;

begin
Test('TestFunc', []);
end. :)

Dgby714
04-22-2011, 07:47 PM
If you would like to pass parameters!

type
TExFunc = function(s: string): boolean;

function test(test2: TExFunc): boolean;

Sex
04-22-2011, 08:03 PM
If you would like to pass parameters!

Simba Code:

type
TExFunc = function(s: string): boolean;

function test(test2: TExFunc): boolean;


Or you can use CallProc :p.

Dgby714
04-22-2011, 08:05 PM
Or you can use CallProc :p.

Yes but using CallProc requires you to make wrappers for Simba's functions.

Sex
04-22-2011, 08:07 PM
Yeah, was gonna just edit that in...but I still think it is nifty :).