comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adambeneschan@gmail.com>
Subject: Re: Function definitions
Date: Fri, 20 Jun 2014 11:19:57 -0700 (PDT)
Date: 2014-06-20T11:19:57-07:00	[thread overview]
Message-ID: <49b56788-d1a4-4304-9e0c-b12336a7512c@googlegroups.com> (raw)
In-Reply-To: <7597c304-0dc2-41ad-b04e-1aef133e5b63@googlegroups.com>

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 need to make Individual a discriminant record:


    type Individual (P, Q, R : Natural) is record 
       X : x_array (0..P); 
       Y : y_array (0..Q); 
       Z : z_array (0..R); 
    end record; 



This in effect puts "parameters" on the record.  The way you tried it did not use parameters; it used variables.  

    P,Q,R : Natural; 

    type Individual is record 
       X : x_array (0..P); 
       Y : y_array (0..Q); 
       Z : z_array (0..R); 
    end record; 

What that does is to use whatever values P, Q, and R happened to have at the time the program reached the type declaration.  Since those variables were uninitialized, they would be garbage, but those garbage values would be used for the array bounds.  And changing P, Q, and R later, or redefining other variables named P, Q, and R, would not have helped.  The arrays in Individual would have those array bounds forever.  That's why you need to tell Ada that you are "parameterizing" the record by putting discriminants on them.  

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 => 1, Q => 1, R => 1);  -- better

The other thing is that instead of declaring F and G in search.ads as separate functions, you need to make them parameters of g_search.  You can do this with a generic, or with an access-to-procedure:

    generic
       with function F (V : Individual) return Float; 
       with function G (M : Positive; V : Individual) return Float; 
    procedure g_search; 

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; 
       with function G (M : Positive; V : Individual) return Float; 
    procedure g_search (Param : Individual); 

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). 

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 => F, G => G);

and now

    This_G_Search(V);

will call your search, and it will use the versions of F and G that you provided. 

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, and also discriminant records, and how parameter passing in Ada works in general.  Right now you seem to be guessing that if you name two variables or two functions with the same name, the language will magically connect everything 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

  reply	other threads:[~2014-06-20 18:19 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-20 15:18 Function definitions montgrimpulo
2014-06-20 15:44 ` Adam Beneschan
2014-06-20 16:22 ` montgrimpulo
2014-06-20 16:43   ` Adam Beneschan
2014-06-20 16:52     ` Simon Clubley
2014-06-20 17:05       ` Adam Beneschan
2014-06-20 16:43   ` Simon Clubley
2014-06-22  6:59   ` Shark8
2014-06-20 17:39 ` montgrimpulo
2014-06-20 18:19   ` Adam Beneschan [this message]
2014-06-20 18:20     ` Adam Beneschan
2014-06-21 20:56     ` Stephen Leake
2014-06-22 12:27     ` Simon Clubley
2014-06-20 20:39   ` Robert A Duff
2014-06-21 12:27 ` montgrimpulo
2014-06-21 12:38   ` Simon Clubley
2014-06-21 17:57   ` Jeffrey Carter
2014-06-21 13:40 ` montgrimpulo
replies disabled

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