f



"out" and "in out"

Hi i found the following explaination:

In Ada, "in" parameters are similar to C++ const parameters. They are
effectively read-only within the scope of the called subprogram.
Ada "in out" parameters have a reliable initial value (that passed
in from the calling subprogram) and may be modified within the scope
of the called procedure. Ada "out" parameters have no reliable
initial value, but are expected to be assigned a value within the
called procedure.

What does "have no reliable initial value" mean when considering the "out"
parameter?

By chance I created a small program as follows:

===========
s : string := "CAT";

procedure modify ( s1 : out string ) is
begin
   s1(2) := 'U';
end modify;

...

put ( modify(s) );
===========

now I get as a result "CUT", and i dont understand why i get this result.
Doesnt the "out" specify that its initial value isnt passed in via the
parameter? But it seems to be passed in the above. In fact the "out" is
acting like an "in out". I am a little confused. Could someone shed some
light on this?

Many thanks!

zork



0
zork5207 (13)
7/26/2004 9:58:22 AM
comp.lang.ada 8774 articles. 2 followers. Post Follow

4 Replies
4756 Views

Similar Articles

[PageSpeed] 2

"zork" <zork@nospam.com> wrote in message news:4104d5de@dnews.tpgi.com.au...
> Hi i found the following explaination:
>
> In Ada, "in" parameters are similar to C++ const parameters. They are
> effectively read-only within the scope of the called subprogram.
> Ada "in out" parameters have a reliable initial value (that passed
> in from the calling subprogram) and may be modified within the scope
> of the called procedure. Ada "out" parameters have no reliable
> initial value, but are expected to be assigned a value within the
> called procedure.
>
> What does "have no reliable initial value" mean when considering the "out"
> parameter?
It means that wihn the procedure "modify," if youo attempt to read the
value of the formal parameter "s1," it would "have no reliable initial
value."
It has no bearing on the value of the actual parameter "s."
>
> By chance I created a small program as follows:
>
> ===========
> s : string := "CAT";
>
> procedure modify ( s1 : out string ) is
> begin
>    s1(2) := 'U';
> end modify;
>
> ..
>
> put ( modify(s) );
> ===========
>
> now I get as a result "CUT", and i dont understand why i get this result.
> Doesnt the "out" specify that its initial value isnt passed in via the
> parameter? But it seems to be passed in the above. In fact the "out" is
> acting like an "in out". I am a little confused. Could someone shed some
> light on this?
>
> Many thanks!
>
> zork
>
>
>
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada-france.org
> http://www.ada-france.org/mailman/listinfo/comp.lang.ada
>
>

0
7/26/2004 11:00:31 AM
zork wrote:

> now I get as a result "CUT", and i dont understand why i get this result.
> Doesnt the "out" specify that its initial value isnt passed in via the
> parameter? But it seems to be passed in the above. In fact the "out" is
> acting like an "in out". I am a little confused. Could someone shed some
> light on this?

No it means: Does not *need* to be passed in. However, if using a pointer
instead of the value gives better performance the compiler will do that.
Also there are types which for technical reasons need to be passed using a
pointer.

What you have done comes under "undefined beahavior". If you used:

type s_type is new string (1 .. 3);

s : s_type = "CAT";

procedure modify ( s1 : out s_type) is
begin
���s1(2)�:=�'U';
end modify; 

things *might* be different on standart CPU's since s_type is smaller then
32 bit. BTW: String is indefinite and size therefore undefined. 

With Regards

Martin

-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com

0
krischik (1357)
7/26/2004 11:30:11 AM
On Mon, 26 Jul 2004 19:58:22 +1000, zork <zork@nospam.com> wrote:

> ...
> By chance I created a small program as follows:
>
> ===========
> s : string := "CAT";
>
> procedure modify ( s1 : out string ) is
> begin
>    s1(2) := 'U';
> end modify;
>
> ..
>
> put ( modify(s) );
> ===========
>
> now I get as a result "CUT", and i dont understand why i get
> this result.

> Doesnt the "out" specify that its initial value isnt passed
> in via the parameter?

Yes, but it doesn't forbid the compiler to (effectively) pass
the value in. In practice, this is likely to happen if the
parameter is of a composite type (an array or record), because
the compiler will probably choose to pass it by reference.

> But it seems to be passed in the above. In fact the "out"
> is acting like an "in out".

Yes. This is permitted. But your program would be wrong to
rely upon it.

If you were to try:

===========
n : integer := 456;

procedure modify ( n1 : out integer ) is
begin
    n1 := n1+44;
end modify;

....

modify(n);
put(n);
===========

I think you might not get 500 printed out (I don't, running
this on GNAT 3.15p on Win XP (and GNAT gives a warning)).

-- 
Nick Roberts
0
nick.roberts (270)
7/26/2004 2:46:37 PM
Thanks for the response everyone. I need to be careful in my implementation.
Some things are obviously not obvious :)

cheers
zork


"Nick Roberts" <nick.roberts@acm.org> wrote in message
news:opsbq1zzd0p4pfvb@bram-2...
> On Mon, 26 Jul 2004 19:58:22 +1000, zork <zork@nospam.com> wrote:
>
> > ...
> > By chance I created a small program as follows:
> >
> > ===========
> > s : string := "CAT";
> >
> > procedure modify ( s1 : out string ) is
> > begin
> >    s1(2) := 'U';
> > end modify;
> >
> > ..
> >
> > put ( modify(s) );
> > ===========
> >
> > now I get as a result "CUT", and i dont understand why i get
> > this result.
>
> > Doesnt the "out" specify that its initial value isnt passed
> > in via the parameter?
>
> Yes, but it doesn't forbid the compiler to (effectively) pass
> the value in. In practice, this is likely to happen if the
> parameter is of a composite type (an array or record), because
> the compiler will probably choose to pass it by reference.
>
> > But it seems to be passed in the above. In fact the "out"
> > is acting like an "in out".
>
> Yes. This is permitted. But your program would be wrong to
> rely upon it.
>
> If you were to try:
>
> ===========
> n : integer := 456;
>
> procedure modify ( n1 : out integer ) is
> begin
>     n1 := n1+44;
> end modify;
>
> ...
>
> modify(n);
> put(n);
> ===========
>
> I think you might not get 500 printed out (I don't, running
> this on GNAT 3.15p on Win XP (and GNAT gives a warning)).
>
> -- 
> Nick Roberts


0
zork5207 (13)
7/28/2004 2:07:48 PM
Reply: