From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: *** X-Spam-Status: No, score=3.8 required=5.0 tests=BAYES_00,INVALID_MSGID, RATWARE_MS_HASH,RATWARE_OUTLOOK_NONAME autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,d04a73d3b2c2dd0d X-Google-Attributes: gid103376,public From: "Nick Roberts" Subject: Re: Q: Pointers to procedures/functions Date: 1997/04/13 Message-ID: <01bc47a0$015c58e0$22f482c1@xhv46.dial.pipex.com>#1/1 X-Deja-AN: 234425349 References: <1997Apr11.143821.5866@news> Organization: UUNet PIPEX server (post doesn't reflect views of UUNet PIPEX) Newsgroups: comp.lang.ada Date: 1997-04-13T00:00:00+00:00 List-Id: Jon S Anthony wrote in article ... > 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.