comp.lang.ada
 help / color / mirror / Atom feed
* Q: Pointers to procedures/functions
@ 1997-04-11  0:00 Gautier
  1997-04-11  0:00 ` Jon S Anthony
  1997-04-12  0:00 ` Jerry van Dijk
  0 siblings, 2 replies; 4+ messages in thread
From: Gautier @ 1997-04-11  0:00 UTC (permalink / raw)



Is there a way to have pointers to procedures in Ada ?

I wish to use e.g. functions float -> float as parameters for a graph
procedure.

G.




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Q: Pointers to procedures/functions
  1997-04-11  0:00 Q: Pointers to procedures/functions Gautier
@ 1997-04-11  0:00 ` Jon S Anthony
  1997-04-13  0:00   ` Nick Roberts
  1997-04-12  0:00 ` Jerry van Dijk
  1 sibling, 1 reply; 4+ messages in thread
From: Jon S Anthony @ 1997-04-11  0:00 UTC (permalink / raw)



In article <1997Apr11.143821.5866@news> Gautier.DeMontmollin@maths.unine.ch (Gautier) writes:

> Is there a way to have pointers to procedures in Ada ?
> 
> I wish to use e.g. functions float -> float as parameters for a graph
> procedure.
> 
> G.

In Ada95, no problem.  Here's a snip from the Rationale:

  "type Trig_Function is access function(F: Float) return Float;

   T: Trig_Function;
   X, Theta: Float;

and T can then "point to" functions such as Sin, Cos and Tan. We can
then assign an appropriate access-to-subprogram value to T by for
example

   T := Sin'Access;"

If you are stuck with Ada(83), the vendor for your compiler has
probably supplied an implementation dependent way to do this.

/Jon

-- 
Jon Anthony
Organon Motives, Inc.
Belmont, MA 02178
617.484.3383
jsa@organon.com





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Q: Pointers to procedures/functions
  1997-04-11  0:00 Q: Pointers to procedures/functions Gautier
  1997-04-11  0:00 ` Jon S Anthony
@ 1997-04-12  0:00 ` Jerry van Dijk
  1 sibling, 0 replies; 4+ messages in thread
From: Jerry van Dijk @ 1997-04-12  0:00 UTC (permalink / raw)



>Is there a way to have pointers to procedures in Ada ?

Yes, see RM 3.10. Example:

   with Ada.Text_IO; use Ada.Text_IO;

   procedure Test is

      type Function_Call is access function (N : Integer) return Integer;

      function Double_It (N : Integer) return Integer is
      begin
         return 2 * N;
      end Double_It;

      function Triple_It (N : Integer) return Integer is
      begin
         return 3 * N;
      end Triple_It;

      procedure Display (N : in Integer; F : in Function_Call) is
      begin
         Put ("Calling function on value" & Integer'Image (N));
         Put (" results in" & Integer'Image (F (N)));
         New_Line;
      end Display;

   begin
      Display (4, Double_It'Access);
      Display (4, Triple_It'Access);
   end Test;

--

-- Jerry van Dijk       | Haarlem, Holland
-- Business Consultant  | Team Ada
-- Ordina Finance       | jdijk@acm.org




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Q: Pointers to procedures/functions
  1997-04-11  0:00 ` Jon S Anthony
@ 1997-04-13  0:00   ` Nick Roberts
  0 siblings, 0 replies; 4+ messages in thread
From: Nick Roberts @ 1997-04-13  0:00 UTC (permalink / raw)





Jon S Anthony <jsa@alexandria> wrote in article
<JSA.97Apr11140450@alexandria>...
> In article <1997Apr11.143821.5866@news>
Gautier.DeMontmollin@maths.unine.ch (Gautier) writes:
> 
> > Is there a way to have pointers to procedures in Ada ?
> > 
> > I wish to use e.g. functions float -> float as parameters for a graph
> > procedure.
> > 
> > G.
> 
> In Ada95, no problem.  Here's a snip from the Rationale:
> 
>   "type Trig_Function is access function(F: Float) return Float;
> 
>    T: Trig_Function;
>    X, Theta: Float;
> 
> and T can then "point to" functions such as Sin, Cos and Tan. We can
> then assign an appropriate access-to-subprogram value to T by for
> example
> 
>    T := Sin'Access;"
> 
> If you are stuck with Ada(83), the vendor for your compiler has
> probably supplied an implementation dependent way to do this.


On the other hand, you can use the generic mechanism in both Ada 83 and Ada
95. This might give you an improvement in speed (although this is likely to
be very small), but, set against this, the flexibility of the method
outlined by Jon Anthony above might be more suited to your requirements.

For the generic approach, you define your graph-drawing procedure as
generic thus:


generic
   with function F(X: Float) return Float;
procedure Draw_Graph;


and then implement the body of Draw_Graph in the normal way, using F as the
name of the function to be graphed. Then you instantiate the procedure
using the different functions you want graphing, e.g.:


procedure Draw_Sin_Graph is new Draw_Graph(Sin);
procedure Draw_Cos_Graph is new Draw_Graph(Cos);


and so on. The functions Sin and Cos in this example must be defined,
before the instantiations, as functions which take one Float parameter and
return one Float parameter, as defined by F in the generic declaration.

Nick.





^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1997-04-13  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-04-11  0:00 Q: Pointers to procedures/functions Gautier
1997-04-11  0:00 ` Jon S Anthony
1997-04-13  0:00   ` Nick Roberts
1997-04-12  0:00 ` Jerry van Dijk

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox