Board index » delphi » Passing functions as parameters in Turbo Pascal

Passing functions as parameters in Turbo Pascal

Hello,

can anybody please explain how I can pass procedures
or functions as parameters to another function in Pascal?
You may also send a sample application that does this.

Thanx,

Dirk

Dirk.Loe...@student.kuleuven.ac.be

 

Re:Passing functions as parameters in Turbo Pascal


In <4inegm$...@chaos.kulnet.kuleuven.ac.be>, Dirk.Loe...@student.kuleuven.ac.be (Dirk Loeckx) writes:

Quote
>Hello,

>can anybody please explain how I can pass procedures
>or functions as parameters to another function in Pascal?
>You may also send a sample application that does this.

>Thanx,

>Dirk

>Dirk.Loe...@student.kuleuven.ac.be

If you want good pascal type checking, try it this way:

  {declare the function you want to pass as a data type }
   type
      myfunc = function (input: Integer): integer;
   end;

  {declare another function which uses your first function as a variable}
   function usefunc(input:integer):integer; FAR;

  {implement it like so}
  function usefunc(input:integer):integer; FAR;
     begin
            {do something}
     end;

 {You can now use your function as a variable like so}
     return := usefunc(myfunc);

If the function you're passing is in a a DLL, you'll have to EXPORT it.

Scott Hanrahan

Re:Passing functions as parameters in Turbo Pascal


On 19 Mar 1996 23:03:50 GMT, Dirk.Loe...@student.kuleuven.ac.be (Dirk

Quote
Loeckx) wrote:
>Hello,

>can anybody please explain how I can pass procedures
>or functions as parameters to another function in Pascal?
>You may also send a sample application that does this.

>Thanx,

>Dirk

>Dirk.Loe...@student.kuleuven.ac.be

You don't need to pass it.
Just call the desired procedure or function when it is needed.
The only restriction is that you must of previously declared the
function or procedure.

Jeff
----------------
We can't all be wrong so I do it for everyone...
----------------

Re:Passing functions as parameters in Turbo Pascal


Quote
Jeff wrote:

> On 19 Mar 1996 23:03:50 GMT, Dirk.Loe...@student.kuleuven.ac.be (Dirk
> Loeckx) wrote:

> >Hello,

> >can anybody please explain how I can pass procedures
> >or functions as parameters to another function in Pascal?
> >You may also send a sample application that does this.

> >Thanx,

> >Dirk

> >Dirk.Loe...@student.kuleuven.ac.be

> You don't need to pass it.
> Just call the desired procedure or function when it is needed.
> The only restriction is that you must of previously declared the
> function or procedure.

> Jeff
> ----------------
> We can't all be wrong so I do it for everyone...
> ----------------

Declare a type with the number of parameters used by function/procedure to be passed.

TYPE
        TrigFunc = Function ( Rad : Real ) : Real;

Procedure UseTrig ( Msg : String, Trig : TrigFunc );

  Begin { UseTrig }
        WriteLn ('Trig function ', Msg, ' result ', Trig(0.23));
  End;  { UseTrig }

Dave

Re:Passing functions as parameters in Turbo Pascal


Quote
David Peterson <dpete...@isd.net> wrote:
>Jeff wrote:

>> On 19 Mar 1996 23:03:50 GMT, Dirk.Loe...@student.kuleuven.ac.be (Dirk
>> Loeckx) wrote:

>> >Hello,

>> >can anybody please explain how I can pass procedures
>> >or functions as parameters to another function in Pascal?
>> >You may also send a sample application that does this.

>> >Thanx,

TYPE
        TrigFunc = Function ( Rad : Real ) : Real;

Procedure UseTrig ( Msg : String, Trig : TrigFunc );

  Begin { UseTrig }
        WriteLn ('Trig function ', Msg, ' result ', Trig(0.23));
  End;  { UseTrig }

Quote

>Dave

Exactly, and now the part on how to use it:

{
 the function is represented by a  far  pointer. This means you have to
 alternatively:
  a) do an $F+
  b) declare the function in the INTERFACE section
  c) declare the function forwards.

Quote
}

{$F+}
function tan(Rad:Real):real:
begin tan:= sin(Rad)/cos(Rad); end;
{$F-}

var f:TrigFunc;
begin
     f:=tan;
     UseTrig('tan',f);    
end.

--
Peter Sch"afer

"office":
schae...@malaga.math.uni-augsburg.de
http://wwwhoppe.math.uni-augsburg.de/schaefer

"leisure":
schae...@mathpool.uni-augsburg.de    
http://www.mathpool.uni-augsburg.de/~schaefer

Re:Passing functions as parameters in Turbo Pascal


hey yur lucky....

Just follow the bouncing code

Potions copywrite 1996 Nuclear Waste Software all rights reseverved

function Figtriangle (base,height:real):real;
{local variables to the function}
{**********************************
accepts the base and hieght of the triangle and performs the
calculations of finding the surface area
***********************************}
begin
       figtriangle := half * base * height;{basic formula for the area
                                      of a triangle}
end;{function}

function figcircle (area,radius:real):real;{local variables to the
function}
{*************************************
accepts the input of the radius to calculate the surface area of a
circle
******************************}
begin
      figcircle := Pi * (sqr(radius));{basic formula for the surface
area
                                       of a circle}
end;{function}

Function figtrap (top,base,height:real):real;
{variables local to the function}
{************************************
Accepts the input of the base, the top and the height of the trapezoid
to calculate the surface area...
*********************************}
begin
      figtrap := half*(base+top)*height
      {formula for the area of a trapezoid}
end;{function}

Procedure Display_menu;
{***************************************
Purpose display the menu of options...
****************************************}
Begin

 Writeln (' The options that are avaiable to you are ');{menu}
 Writeln (' "0" to exit the program.');                 {menu}
 Writeln (' "1" To obtain the surface area of a Triangle. '); {menu}
 Writeln (' "2" To obtain the surface area of a Circle. ');   {menu}
 Writeln (' "3" To obtain the surface area of a Trapezoid. ');{menu}
 End;{display_menu procedure}

 procedure triangle
{********************************
Computes the surface area of a triangle
input/output...
*********************************};
Begin

 Writeln ('Please enter the base length and the height of the
triangle');
{displays to the screen}
 Readln (base, height);{reads the variable input}
 write ('The surface area is ',figtriangle(base,height):7:2);
 {writes the computations to the screen}
 Writeln ( ' units.');{display to the screen}
 writeln;{blank line}
 display_menu {calls the procedure variable display_menu}

End; {end procedure triangle}

Procedure circle
{******************************
Calculates the suface area of a circle
input/output
*******************************};
Begin
Writeln ('Please enter the radius of the circle.');{prompt user for
input}
readln (radius);                                   {reads input}
write ('The surface area is ',figcircle(pi,radius):7:2);
                                            {displays to the screen}
Writeln;                                    {blank line}
display_menu                                {calls the display_menu}
end; {end procedure}

procedure trap
{*******************************
Calculates the surface area of a trapezoid
input/output
********************************};
Begin
Writeln ('Please enter the lengths of the base, the top and ');
{prompt the user for input}
Writeln ('the height of the trapezoid you wish to find the');
{displays to screen}
writeln ('              surface area of.');{display to the screen}
readln (base, top, height );                {reads input}
write ('The surface area is ',figtrap (base,top,height):7:2);
{displays to the screen}
Writeln; {blank screen}
display_menu {calls the display_menu procedure}
end;{procedure}

Begin
 {******************************MAIN PROGRAM*********************}

     Writeln; {displays a blank line}
     Writeln ('         Nuclear Waste Software Presents, ');{Header}
     Writeln ('                  the home                  ');
     Writeln  ('           Surface Area Computer!  ');{header}
     Writeln ; {displays a blank line}
     Writeln ('                        _ _ /a     Ack Pffft
Thwerrp!!');
     {my logo}
     Writeln ('                       \ o.O |
');
     {my logo}
     Writeln ('                       =(___)=
');
     {my logo}
     Writeln ('                          U
');
     {my logo}
     Writeln; {blank line}
     Display_menu;
     Writeln ('Please enter your option');{prompts the user for input}
     Writeln; {blank line}
     readln (option); {reads input of the option}
     while option <> 0 do
     begin
     if option = 1 then
     triangle
     else if option = 2 then
     circle
     else if option = 3 then
     trap
     else
     writeln ('Warning! Invalid option, please re-enter your
option.');
     writeln;
     readln (option);
     end; {while}

End.
****************************************************
* Secrecy is the keystone to all tyranny. Not force,
* but secrecy and censorship. When any government    
* or church for that matter, undertakes to say to    
* it's subjects, "This you may not read, this you    
* must not know,"   the end result is tyranny and    
* oppression, no matter how holy the motives.        
* Mighty little force is needed to control a man who
* has been hoodwinked in this fashion; contrariwise,
* no amount of force can control a free man, whose  
* mind is free.  No, not the rack nor the atomic    
* bomb, not anything. you can't conquer a free man;  
* the most you can do is kill him.                  
*                                                    
* Robert Anson Heinlein  "If this goes on"  1939    
****************************************************
      AJ...@LAFN.ORG PHUQ the decency act!

Other Threads