comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: From Pascal to Ada
Date: Thu, 01 Nov 2007 16:45:40 -0700
Date: 2007-11-01T16:45:40-07:00	[thread overview]
Message-ID: <1193960740.756224.6960@y27g2000pre.googlegroups.com> (raw)
In-Reply-To: <1193957079.981952.101030@d55g2000hsg.googlegroups.com>

On Nov 1, 3:44 pm, j.kha...@oltrelinux.com wrote:
> Hi,
> how do you translate this Pascal code in Ada?
>
> type
>   realFunction = function(x: double): double;

Ada doesn't really have types that denote *functions*, but instead
they have types that are *access* to function (sort of like a
pointer).  Assuming you have a type named Double (in Ada, the standard
floating-point types are named Float and Long_Float):

   type Real_Function is access function (X : Double) return Double;

(Note: Ada programmers tend not to go for pseudo-Germanic or Hungarian
notation when naming things.)  If you have a function like Cos that
takes a Double and returns a Double, you'd say Cos'Access to get a
"pointer" to the function.

Now, you can not only pass this as a procedure or function parameter,
you can also declare variables of this type and store function
pointers in them.  Because of this ability, there are restrictions on
what kinds of functions you can take the 'Access of.  Basically, if
Func is nested inside some other procedure Proc, you can't use
Func'Access where a Real_Function is needed, unless the definition of
Real_Function is also nested inside that procedure Proc.  (That's an
oversimplification of the rule.)  Otherwise you could call Func while
you're not even inside Proc and really screw yourself up.

But if all you want to do is pass this function to a procedure, you
can use an *anonymous* access type:

   procedure Do_Integrate (Func : access function (X : Double) return
Double);

and now you can use Do_Integrate(Cos'Access) or whatever other
function you want, even if it's nested.  But you can't give a name to
the access-function type in that case.

Hope this helps,

                           -- Adam







  parent reply	other threads:[~2007-11-01 23:45 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
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 [this message]
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