comp.lang.ada
 help / color / mirror / Atom feed
From: "Nick Roberts" <Nick.Roberts@dial.pipex.com>
Subject: Re: Q: Pointers to procedures/functions
Date: 1997/04/13
Date: 1997-04-13T00:00:00+00:00	[thread overview]
Message-ID: <01bc47a0$015c58e0$22f482c1@xhv46.dial.pipex.com> (raw)
In-Reply-To: JSA.97Apr11140450@alexandria




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.





  reply	other threads:[~1997-04-13  0:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
1997-04-12  0:00 ` Jerry van Dijk
replies disabled

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