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-Thread: 103376,dd17afdec1033c93 X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!postnews.google.com!y27g2000pre.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: From Pascal to Ada Date: Thu, 01 Nov 2007 16:45:40 -0700 Organization: http://groups.google.com Message-ID: <1193960740.756224.6960@y27g2000pre.googlegroups.com> References: <1193957079.981952.101030@d55g2000hsg.googlegroups.com> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" X-Trace: posting.google.com 1193960741 3008 127.0.0.1 (1 Nov 2007 23:45:41 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 1 Nov 2007 23:45:41 +0000 (UTC) In-Reply-To: <1193957079.981952.101030@d55g2000hsg.googlegroups.com> User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: y27g2000pre.googlegroups.com; posting-host=66.126.103.122; posting-account=ps2QrAMAAAA6_jCuRt2JEIpn5Otqf_w0 Xref: g2news2.google.com comp.lang.ada:2701 Date: 2007-11-01T16:45:40-07:00 List-Id: 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