[comp.lang.pascal] Procedures and functions as parameters to other procs/funcs

John G. Spragge <SPRAGGEJ@QUCDN.QueensU.CA> (02/04/91)

Try this:

Type x = FUNCTION (x : integer) : INTEGER;

FUNCTION a (x : INTEGER) : INTEGER;

BEGIN
   a := x * 2;   { or anything else you want }
END;

FUNCTION b ((x : INTEGER) : INTEGER;

BEGIN
   b := x DIV 2; { or anything you want }
END;

PROCEDURE c (f : x);

BEGIN
   WRITELN (f (10));
END;

BEGIN
   c (a);
   c (b)
END.

That should work (let me know if it doesn't).

disclaimer: Queen's University merely supplies me with computer services, and
            they are responsible for neither my opinions or my ignorance.

John G. Spragge

ballerup@diku.dk (Per Goetterup) (02/04/91)

I was also fooling around with this concept, in TP 6.0 though. I was trying
to do something like this:

 function a(x: integer): integer;
 begin
   { do something }
 end;

 function b(x: integer): integer;
 begin
   { do something else }
 end;

 procedure qaz(var f: function; .... );
 begin
   writeln(f(23));
 end;

 begin 
   qaz(a, ...);
   qaz(b, ...);
 end.

TP 6.0 won't let me do that. Then I tried this version of qaz:

 procedure qaz(p: pointer; ... );
 var
   f: function(x: integer): integer absolute p;
 begin
   writeln(f(23));
 end;

This works - in a way. Both functions a and b gets called in the right
order, BUT the value of x inside them is wrong! - After several (lots) of
calls, the program (and TP) crashes, thus indicating stack problems.

Has anybody else tried this in TP? - As TP allow that VAR-declaration, it
must be supported in some way or other. Removing the parameter in the
declaration of f, or making it a VAR-parameter doesn't work either because
the program can't be compiled like that.

Hints? - Use objects perhaps? (HOW?)

	Anything would be appreciated!

		Per.

-- 
| Per Gotterup                        | "The most merciful thing in the    |
| Student, DIKU (Dept. of Comp. Sci.) | world, I think, is the inability   |
| University of Copenhagen, Denmark   | of the human mind to correlate all |
| Internet: ballerup@freja.diku.dk    | its contents."  - H.P. Lovecraft - |