comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: From Pascal to Ada
Date: Fri, 02 Nov 2007 00:38:34 +0100
Date: 2007-11-02T00:38:34+01:00	[thread overview]
Message-ID: <877il1cz9h.fsf@ludovic-brenta.org> (raw)
In-Reply-To: 1193957079.981952.101030@d55g2000hsg.googlegroups.com

j.khaldi@oltrelinux.com writes:
> Hi,
> how do you translate this Pascal code in Ada?
>
> type
>   realFunction = function(x: double): double;
>
> Thanks!

In Ada you cannot declare subprogram types, only access-to-subprogram
(aka "pointer to subprogram") types.  In this case, you would say

declare
   type Real_Function is access function (X : Long_Float) return Long_Float;

   function F (X : Long_Float) return Long_Float is
   begin
      return 42.0 * X;
   end F;

   type Vector is array (Positive range <>) of Long_Float;
   procedure Apply (Func : in Real_Function; To : in out Vector) is
   begin
      for J in To'Range loop
         To (J) := Func (To (J)); -- call the pointed-to function
      end loop;
   end Apply;

   V : Vector (1 .. 100);
begin
   Apply (Func => F'Access, To => V);
end;

However there are other ways.  Ada 2005 introduces anonymous
access-to-subprogram types:

procedure Apply (Func : access function (X : Long_Float) return Long_Float;
                 To : in out Vector);

And it has been possible to use generics since Ada 83:

generic
   with function Func (X : Long_Float) return Long_Float;
procedure Apply (To : in out Vector);

Why do you ask?

-- 
Ludovic Brenta.



  reply	other threads:[~2007-11-01 23:38 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-11-01 22:44 From Pascal to Ada j.khaldi
2007-11-01 23:38 ` Ludovic Brenta [this message]
2007-11-03 13:29   ` j.khaldi
2007-11-03 13:51     ` Ludovic Brenta
2007-11-01 23:44 ` anon
2007-11-01 23:45 ` Adam Beneschan
2007-11-03 13:04   ` j.khaldi
2007-11-02  0:01 ` (see below)
2007-11-02  1:02   ` Adam Beneschan
replies disabled

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