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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,ae33d52681d12ef3 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-06-19 05:52:04 PST Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!paloalto-snf1.gtei.net!crtntx1-snh1.gtei.net!lsanca1-snf1!news.gtei.net!newsfeed2.earthlink.net!newsfeed.earthlink.net!newsmaster1.prod.itd.earthlink.net!newsread1.prod.itd.earthlink.net.POSTED!not-for-mail Message-ID: <3B2F3B9A.B0ADDE22@earthlink.net> From: "Marc A. Criley" Organization: Quadrus Corporation X-Mailer: Mozilla 4.73 [en] (X11; U; Linux 2.2.14-5.0 i686) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Returning a function References: <9gn68f$6j6@news.or.intel.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Date: Tue, 19 Jun 2001 12:40:59 GMT NNTP-Posting-Host: 63.178.180.30 X-Complaints-To: abuse@earthlink.net X-Trace: newsread1.prod.itd.earthlink.net 992954459 63.178.180.30 (Tue, 19 Jun 2001 05:40:59 PDT) NNTP-Posting-Date: Tue, 19 Jun 2001 05:40:59 PDT X-Received-Date: Tue, 19 Jun 2001 05:38:54 PDT (newsmaster1.prod.itd.earthlink.net) Xref: archiver1.google.com comp.lang.ada:8889 Date: 2001-06-19T12:40:59+00:00 List-Id: "Hoffmann, Torben" wrote: > > Hi. > > I would like to do some higher-order functions in Ada (just for the fun of > it) - could anyone give me a link which provides information on how to > return a function from a function (or procedure). > > Thanks in advance > /Torben Here's a quick example: procedure FF is type Operations is (Check_Positive, Check_Even); type A_Function_Type is access function(X : Integer) return Boolean; function Is_Positive (A : Integer) return Boolean is begin return A > 0; end Is_Positive; function Is_Even (Z : Integer) return Boolean is begin return (Z mod 2) = 0; end Is_Even; function Select_Function(Op : Operations) return A_Function_Type is begin if Op = Check_Positive then return Is_Positive'Access; elsif Op = Check_Even then return Is_Even'Access; end if; end Select_Function; AFT : A_Function_Type; Result : Boolean; begin -- Storing the function in a variable, and then -- invoking it. AFT := Select_Function(Check_Positive); Result := AFT.all(-19); AFT := Select_Function(Check_Even); Result := AFT.all(-19); -- Calling it directly via the retrieval function Result := Select_Function(Check_Positive).all(38); end FF; Marc A. Criley Senior Staff Engineer Quadrus Corporation www.quadruscorp.com