* in-out and out parameters
@ 2004-02-26 14:00 Evangelista Sami
2004-02-26 14:18 ` Frank J. Lhota
` (3 more replies)
0 siblings, 4 replies; 6+ messages in thread
From: Evangelista Sami @ 2004-02-26 14:00 UTC (permalink / raw)
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.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: in-out and out parameters
2004-02-26 14:00 in-out and out parameters Evangelista Sami
@ 2004-02-26 14:18 ` Frank J. Lhota
2004-02-26 14:23 ` Jacob Sparre Andersen
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Frank J. Lhota @ 2004-02-26 14:18 UTC (permalink / raw)
"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.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: in-out and out parameters
2004-02-26 14:00 in-out and out parameters Evangelista Sami
2004-02-26 14:18 ` Frank J. Lhota
@ 2004-02-26 14:23 ` Jacob Sparre Andersen
2004-02-26 14:29 ` Jean-Pierre Rosen
2004-02-26 14:30 ` Hyman Rosen
3 siblings, 0 replies; 6+ messages in thread
From: Jacob Sparre Andersen @ 2004-02-26 14:23 UTC (permalink / raw)
Evangelista Sami wrote:
> I thought that, as I and J were passed by references,
Why? And if you want make sure that happen, then you should write it
explicitly using access types.
Jacob
--
"Nobody writes jokes in base 13."
Douglas Adams
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: in-out and out parameters
2004-02-26 14:00 in-out and out parameters Evangelista Sami
2004-02-26 14:18 ` Frank J. Lhota
2004-02-26 14:23 ` Jacob Sparre Andersen
@ 2004-02-26 14:29 ` Jean-Pierre Rosen
2004-02-26 14:30 ` Hyman Rosen
3 siblings, 0 replies; 6+ messages in thread
From: Jean-Pierre Rosen @ 2004-02-26 14:29 UTC (permalink / raw)
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 616 bytes --]
"Evangelista Sami" <evangeli@cnam.fr> a �crit dans le message de news:5f59677c.0402260600.3c27ad66@posting.google.com...
> hello
>
> i have this program :
[snip]
> 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.
I and J are elementary types, thus passed by copy. 6.2(3).
--
---------------------------------------------------------
J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: in-out and out parameters
2004-02-26 14:00 in-out and out parameters Evangelista Sami
` (2 preceding siblings ...)
2004-02-26 14:29 ` Jean-Pierre Rosen
@ 2004-02-26 14:30 ` Hyman Rosen
2004-02-27 23:54 ` Randy Brukardt
3 siblings, 1 reply; 6+ messages in thread
From: Hyman Rosen @ 2004-02-26 14:30 UTC (permalink / raw)
Evangelista Sami wrote:
> I thought that, as I and J were passed by references
In Ada, out parameters may be passed using copy-in/copy-out
rather than by reference in certain circumstances.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: in-out and out parameters
2004-02-26 14:30 ` Hyman Rosen
@ 2004-02-27 23:54 ` Randy Brukardt
0 siblings, 0 replies; 6+ messages in thread
From: Randy Brukardt @ 2004-02-27 23:54 UTC (permalink / raw)
"Hyman Rosen" <hyrosen@mail.com> wrote in message
news:1077805821.190885@master.nyc.kbcfp.com...
> Evangelista Sami wrote:
> > I thought that, as I and J were passed by references
>
> In Ada, out parameters may be passed using copy-in/copy-out
> rather than by reference in certain circumstances.
Which are all of them in this case (elementary types are *always* passed by
copy-in/copy-out). Ada has types that must always be passed by reference as
well. And there are some types where the compiler is allowed to choose - but
the compiler is likely to make the same choice for all parameter passing
modes (for instance, if the objects are large).
Thus, the choice between by-reference and by-copy depends mostly on the type
of the item passed; the parameter passing mode has little or no effect on
how items are passed.
Randy.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2004-02-27 23:54 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-02-26 14:00 in-out and out parameters Evangelista Sami
2004-02-26 14:18 ` Frank J. Lhota
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox