comp.lang.ada
 help / color / mirror / Atom feed
From: "Frank J. Lhota" <NOSPAM.lhota.adarose@verizon.net>
Subject: Re: in-out and out parameters
Date: Thu, 26 Feb 2004 14:18:52 GMT
Date: 2004-02-26T14:18:52+00:00	[thread overview]
Message-ID: <gfn%b.8654$ax2.2344@nwrdny03.gnilink.net> (raw)
In-Reply-To: 5f59677c.0402260600.3c27ad66@posting.google.com

"Evangelista Sami" <evangeli@cnam.fr> wrote in message
news:5f59677c.0402260600.3c27ad66@posting.google.com...
> hello
>
> i have this program :
> ====================
> procedure Test is
>
>    task T is
>       entry E(I :    out Integer;
>               J : in out Integer);
>    end T;
>    task body T is
>    begin
>       accept E(I :    out Integer;
>                J : in out Integer) do
>          I := 1;
>          J := J * I;
>          J := J * J;
>       end E;
>    end;
>
>    I : Integer := 10;
>
> begin
>    T.E(I, I);
>    Put_Line(Integer'Image(I));
> end;
> ====================
> The output is 1.
> I thought that, as I and J were passed by references, when i call E
> with the
> same variables, I and J denote the same object. so i was expecting to
> get 100 in output.
> If someone had a clear explanation i would be very thankful.

Actually, you are probably getting this result precisely BECAUSE call by
reference is being used. With call by reference, the assignment statement

    I := 1;

in the "accept" statement will set Test.I to 1 as well. After this
assignment, both the E parameters I and J will be 1, and hence I and J will
return 1.

This is not meant to be an indictment of call by reference. The real problem
is that the entry call itself is confusing: if the values computed for the I
and J parameters are different, which value should end up in Test.I? A more
straightforward example, assume we have the procedure

    procedure Foo( L : out Integer; R : out Integer ) is
    begin
       L := 1;
       R := 2;
    end Foo;

and an integer variable I, what is the value of I after the call

    Foo( I, I );

This is not the sort of question that should be burdening those who maintain
your code. Avoid these confusing procedure / entry calls and the issue of
calling mechanism goes away.





  reply	other threads:[~2004-02-26 14:18 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-02-26 14:00 in-out and out parameters Evangelista Sami
2004-02-26 14:18 ` Frank J. Lhota [this message]
2004-02-26 14:23 ` Jacob Sparre Andersen
2004-02-26 14:29 ` Jean-Pierre Rosen
2004-02-26 14:30 ` Hyman Rosen
2004-02-27 23:54   ` Randy Brukardt
replies disabled

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