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,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.58.65.38 with SMTP id u6mr2050884ves.16.1403288398388; Fri, 20 Jun 2014 11:19:58 -0700 (PDT) X-Received: by 10.50.7.97 with SMTP id i1mr113102iga.8.1403288398167; Fri, 20 Jun 2014 11:19:58 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!peer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!i13no7368403qae.1!news-out.google.com!gf2ni9igb.0!nntp.google.com!uq10no1284132igb.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Fri, 20 Jun 2014 11:19:57 -0700 (PDT) In-Reply-To: <7597c304-0dc2-41ad-b04e-1aef133e5b63@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=66.126.103.122; posting-account=KSa2aQoAAACOxnC0usBJYX8NE3x3a1Xq NNTP-Posting-Host: 66.126.103.122 References: <610b9d5b-a9a5-464d-9de3-b2f754f58cff@googlegroups.com> <7597c304-0dc2-41ad-b04e-1aef133e5b63@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <49b56788-d1a4-4304-9e0c-b12336a7512c@googlegroups.com> Subject: Re: Function definitions From: Adam Beneschan Injection-Date: Fri, 20 Jun 2014 18:19:58 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Received-Bytes: 4677 X-Received-Body-CRC: 2548846365 Xref: news.eternal-september.org comp.lang.ada:20478 Date: 2014-06-20T11:19:57-07:00 List-Id: On Friday, June 20, 2014 10:39:37 AM UTC-7, montgrimpulo wrote: > Here is some code: Thank you, it does make things a lot clearer. First of all, if you want the bounds of Individual to be flexible, you'll n= eed to make Individual a discriminant record: type Individual (P, Q, R : Natural) is record=20 X : x_array (0..P);=20 Y : y_array (0..Q);=20 Z : z_array (0..R);=20 end record;=20 This in effect puts "parameters" on the record. The way you tried it did n= ot use parameters; it used variables. =20 P,Q,R : Natural;=20 type Individual is record=20 X : x_array (0..P);=20 Y : y_array (0..Q);=20 Z : z_array (0..R);=20 end record;=20 What that does is to use whatever values P, Q, and R happened to have at th= e time the program reached the type declaration. Since those variables wer= e uninitialized, they would be garbage, but those garbage values would be u= sed for the array bounds. And changing P, Q, and R later, or redefining ot= her variables named P, Q, and R, would not have helped. The arrays in Indi= vidual would have those array bounds forever. That's why you need to tell = Ada that you are "parameterizing" the record by putting discriminants on th= em. =20 Then, later, when you declare a variable of that type: V : Individual; -- wrong you have to specify the values of P, Q, and R: V : Individual (1, 1, 1); V : Individual (P =3D> 1, Q =3D> 1, R =3D> 1); -- better The other thing is that instead of declaring F and G in search.ads as separ= ate functions, you need to make them parameters of g_search. You can do th= is with a generic, or with an access-to-procedure: generic with function F (V : Individual) return Float;=20 with function G (M : Positive; V : Individual) return Float;=20 procedure g_search;=20 F and G are generic parameters. I don't know if you want P, Q, and R to be= parameters of g_search or not, since it still isn't clear what you want. = If g_search is supposed to search one of these records, you'll need to give= it a record as a parameter: generic with function F (V : Individual) return Float;=20 with function G (M : Positive; V : Individual) return Float;=20 procedure g_search (Param : Individual);=20 But you don't need to pass P, Q, and R, because you can get those from the = parameter (Param.P, Param.Q, Param.R).=20 Then you can't call g_search directly; you have to instantiate it. function F(V : Individual) return Float is ... end F; function G(M : Individual) return Float is ... end G; function This_G_Search is new G_Search(F =3D> F, G =3D> G); and now This_G_Search(V); will call your search, and it will use the versions of F and G that you pro= vided.=20 Another possible way is to use access-to-procedure types. But I think that= for now, you need to go back and do more study about how generics work, an= d also discriminant records, and how parameter passing in Ada works in gene= ral. Right now you seem to be guessing that if you name two variables or t= wo functions with the same name, the language will magically connect everyt= hing together. But it doesn't work like that. That's why I think you need= to back up a little and do some more reading about how Ada actually works. -- Adam