comp.lang.ada
 help / color / mirror / Atom feed
From: jerry@jvdsys.nextjk.stuyts.nl (Jerry van Dijk)
Subject: Re: Function Pointers
Date: 1998/04/14
Date: 1998-04-14T00:00:00+00:00	[thread overview]
Message-ID: <ErEsxx.9F@jvdsys.nextjk.stuyts.nl> (raw)
In-Reply-To: 353257CC.211B84EE@phaseiv.com


Barry L. Dorough (bdorough@phaseiv.com) wrote:

: Is there any way to implement function pointers in Ada?

Yes, basically the same way as in C. However, being a stronger
typed language, Ada asks you to explicitly create a pointer type
for the functions you want to access through a pointer, so that the
compiler can check argument and return types.

: Below is a
: sample C program using function pointers.  How can I do this in Ada?

Actually, your program is C++, not C. Anyway:

-- As GNAT allows only one compilation unit in a file
--  we nest the functions within main;
-- Long_Float would be GNAT's equivalent to gcc's double;
-- We pretend that Main is a 'void' function. 

with Ada.Text_IO; use Ada.Text_IO;

procedure Main is

   type Func_Ptr is access function (X, Y : Integer) return Integer;
   -- Declare a pointer type, pointing to a function that takes
   -- two Integer variables as input and returns a Integer

   function Add (X, Y : Integer) return Integer is
   begin
      return X + Y;
   end Add;

   function Subtract (X, Y : Integer) return Integer is
   begin
      return X - Y;
   end Subtract;

   function Combine (P, Q : Integer; Func : Func_Ptr) return Integer is
      A : Integer := 10 + P;
   begin
      return Func (A, Q);
   end Combine;

   W : Integer := 20;
   X : Integer := 5;
   Y : Integer := Combine (W, X, Add'Access);
   Z : Integer := Combine (W, X, Subtract'Access);

begin
   Put_Line (W'Img & " " & X'Img & " " & Y'Img & " " & Z'Img);
end Main;
-- 
-- Jerry van Dijk  | email: jdijk@acm.org
-- Leiden, Holland | member Team-Ada




  parent reply	other threads:[~1998-04-14  0:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-04-13  0:00 Function Pointers Barry L. Dorough
1998-04-13  0:00 ` Matthew Heaney
1998-04-14  0:00 ` Jerry van Dijk [this message]
1998-04-15  0:00   ` Robert Dewar
1998-04-16  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