comp.lang.ada
 help / color / mirror / Atom feed
From: "Marc A. Criley" <mcqada@earthlink.net>
Subject: Re: Returning a function
Date: Tue, 19 Jun 2001 12:40:59 GMT
Date: 2001-06-19T12:40:59+00:00	[thread overview]
Message-ID: <3B2F3B9A.B0ADDE22@earthlink.net> (raw)
In-Reply-To: 9gn68f$6j6@news.or.intel.com

"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



  reply	other threads:[~2001-06-19 12:40 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-06-19  8:56 Returning a function Hoffmann, Torben
2001-06-19 12:40 ` Marc A. Criley [this message]
2001-06-26  2:07 ` Arthur G. Duncan
replies disabled

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