From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Date: 3 May 93 20:36:27 GMT From: wdl39!mab@ford-wdl1.arpa (Mark A Biggar) Subject: Re: Passing procedures as parameters to procedures. Message-ID: <1993May3.203627.29250@wdl.loral.com> List-Id: In article <1993May3.190746.1043@ee.ubc.ca> luisl@ee.ubc.ca (luis linares-rojas ) writes: >Subject: Procedures/functions as parameters to procedures/functions. >Before commiting myself to any long term commitment with the Ada language, >I've been revising its capabilities. There is something that I have not >found so far: how to pass a procedure (or a function) as a parameter to anothe r >procedure (or function). Or, in the same spirit, how to create an array of >procedures. The question is: is there a way of doing this in Ada, and (if so) >how? I'd appreciate any help. First Ada9x is adding access to procedure/function types so that is a non-problem in Ada9x. In Ada83, it is not quit so simple. First notice that as there is no way to dynamicly create procedures at run time, the set of procedures (include functions here as well) that could be passed as a parameter or used in an array is statically known at compile time. So for passing procedures just compile it up as a generic and locally instantiate the generic in the local declarative region where you need to call the parameterized routine. For example, the usual integrate example: generic with function f(x: float) return float is <>; function integrate(low: float; high: float); function integrate(low: float; high: float) is begin ... end integrate; function sin(x: float) return float is ...; function cos(x: float) return float is ...; Then to use it somewhere just instantiate it like so: declare function int_sin(l: float; h: float) is new integrate(sin); begin ... -- code that call int_sin end; Now for the array of pointers case your only option it to write a procedure containing a big case statement, but thats functiuonaly equivalent isn't it. If you need to change behavior on the fly, just include all possible callable procedures in the case statememnt and use a mapping function call as the controlling expression for the case statement. Suitable inline pragmas and a compiler optimizer that knows how to do goto and sub-call chaining, and this can be just as effecient as the original array of pointers to procedures. -- Mark Biggar mab@wdl1.wdl.loral.com