Board index » delphi » Pass Functions into Function?

Pass Functions into Function?

Does anyone know how to pass functions as function parameters in Borland
Pascal 7? I wrote a simple program, but I could not get it to compile.
What is wrong with my code? Thanks in advance.

program test;
        type
                MathFunc = function (x: real): real;

        function dot (f, g: MathFunc, x: real): real;
        begin
                dot := f(x)  * g(x);
        end;

begin
        writeln (dot (sin, cos, 5));
end.

--
K...@SEAS.UCLA.EDU
http://www.seas.ucla.edu/~kin
===============================================================================
"#define QUESTION ((bb) || !(bb))  - Shakespeare."
===============================================================================

 

Re:Pass Functions into Function?


In article <3u2rc9$...@saba.info.ucla.edu> k...@cloudburst.seas.ucla.edu (Kin Lau)

Quote
 writes:
> Does anyone know how to pass functions as function parameters in Borland
> Pascal 7? I wrote a simple program, but I could not get it to compile.
> What is wrong with my code? Thanks in advance.

The problem with your code is that in Borland Pascal, you may not
pass standard functions as parameter (they are handled specially
by the compiler to generate more efficient code, and this code
prevent to use them as function parameter).

So try the following program (not checked)

Quote
> program test;
>    type
>            MathFunc = function (x: real): real;

        function mysin (x : real) : real; far;
        begin
           mysin := sin (x);
        end; { mysin }

        function mycos (x : real) : real; far;
        begin
          mycos := cos (x);
        end; { mycos }

Quote
>    function dot (f, g: MathFunc, x: real): real;
>    begin
>            dot := f(x)  * g(x);
>    end;

> begin
>    writeln (dot (sin, cos, 5));

        writeln (dot (mysin, mycos, 5));

Quote
> end.

> --
> K...@SEAS.UCLA.EDU
> http://www.seas.ucla.edu/~kin
> ===============================================================================
> "#define QUESTION ((bb) || !(bb))  - Shakespeare."
> ===============================================================================

I hope this help.

-----------------------------------------------------------------------
 Jean-Marc Bourguet                      Service d'Electronique
                                         Faculte polytechnique de Mons
 Email : bourg...@muelec.fpms.ac.be      Belgique

--
-----------------------------------------------------------------------
 Jean-Marc Bourguet                      Service d'Electronique
                                         Faculte polytechnique de Mons
 Tel   : +32 65 37.42.23                 Boulevard Dolez, 31
 Fax   : +32 65 37.42.36                 7000 Mons
 Email : bourg...@muelec.fpms.ac.be      Belgique

Re:Pass Functions into Function?


Quote
Kin Lau (k...@cloudburst.seas.ucla.edu) wrote:

: Does anyone know how to pass functions as function parameters in Borland
: Pascal 7? I wrote a simple program, but I could not get it to compile.
: What is wrong with my code? Thanks in advance.
[...]
: begin
:       writeln (dot (sin, cos, 5));
: end.

Some libraries that I use expect function parameters. I'm not sure of the
details, but BP7 generally expects you to put the address operator (@) in
front of function names when you pass them as parameters, e.g.
writeln(dot(@sin,@cos,5)). This may or may not be the problem here.

Hope this sheds some light on the subject.

Andrew Klopper (andr...@teklogic.co.za)

Other Threads