comp.lang.ada
 help / color / mirror / Atom feed
* Question on in/out parameters
@ 2022-04-30  8:57 reinert
  2022-04-30  9:38 ` Jeffrey R.Carter
  2022-05-02 21:35 ` Randy Brukardt
  0 siblings, 2 replies; 5+ messages in thread
From: reinert @ 2022-04-30  8:57 UTC (permalink / raw)


Hello,

I expected an "out" parameter in a procedure to be like declaring the parameter "from scratch" (with actual initial default value). For my compiler (GNAT Community Edition, May 2021) it seems like the out parameters brings in content from the calling procedure. Should it be like this?

Below is a test program to illustrate.

reinert

with Ada.Containers; use Ada.Containers;
with Ada.Containers.Vectors;
with Ada.Text_IO; use Ada.Text_IO;
procedure test2 is

   package Integer_Vectors is new Ada.Containers.Vectors
       (Index_Type   => Natural, Element_Type => Integer);
   use Integer_Vectors;

   V : Vector := 10 & 20;

   procedure rk_in_out(W : in out Vector) is
   begin
     W.Append(30); W.Append(40);
   end rk_in_out;

   procedure rk_out(W :    out Vector) is
   begin
     W.Clear;  -- I expected this statement to be redundant since W is "out parameter" (try to remove it and see if results remain the same.)?
     W.Append(30); W.Append(40);
   end rk_out;

begin

   New_Line;
   Put ("First V  : ");
   for e of V loop
       Put(e'Image & " ");
   end loop;

   rk_in_out(W => V);
   New_Line;
   Put ("Second V : ");
   for e of V loop
       Put(e'Image & " ");
   end loop;

   rk_out(W => V);
   New_Line;
   Put ("Third V :  ");
   for e of V loop
       Put(e'Image & " ");
   end loop;

end test2;


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Question on in/out parameters
  2022-04-30  8:57 Question on in/out parameters reinert
@ 2022-04-30  9:38 ` Jeffrey R.Carter
  2022-04-30 11:30   ` reinert
  2022-05-02 21:35 ` Randy Brukardt
  1 sibling, 1 reply; 5+ messages in thread
From: Jeffrey R.Carter @ 2022-04-30  9:38 UTC (permalink / raw)


On 2022-04-30 10:57, reinert wrote:
> 
> I expected an "out" parameter in a procedure to be like declaring the parameter "from scratch" (with actual initial default value). For my compiler (GNAT Community Edition, May 2021) it seems like the out parameters brings in content from the calling procedure. Should it be like this?

Parameters in Ada are either passed by copy or passed by reference, regardless 
of parameter mode. The rules are

* Scalar types are always passed by copy
* Tagged types are always passed by reference
* Limited types are always passed by reference
* All other types are decided by the compiler

For the types that are passed by reference, "in out" and "out" mode are identical.

Vector is a tagged type, so this applies to it.

One can argue that an out-mode parameter of a by-reference type should be 
"reinitialized" before use, but the Ada-9X revision decided not to require this.

-- 
Jeff Carter
"Hello! Smelly English K...niggets."
Monty Python & the Holy Grail
08

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Question on in/out parameters
  2022-04-30  9:38 ` Jeffrey R.Carter
@ 2022-04-30 11:30   ` reinert
  2022-05-01  7:50     ` J-P. Rosen
  0 siblings, 1 reply; 5+ messages in thread
From: reinert @ 2022-04-30 11:30 UTC (permalink / raw)


Thanks for sorting out my confusion. 

So there is no difference between "in", "in out" and "out" for Vectors except that the compiler protests if I try to change an "in parameter" in the actual subroutine/function?

reinert


lørdag 30. april 2022 kl. 11:38:08 UTC+2 skrev Jeffrey R.Carter:
> On 2022-04-30 10:57, reinert wrote: 
> > 
> > I expected an "out" parameter in a procedure to be like declaring the parameter "from scratch" (with actual initial default value). For my compiler (GNAT Community Edition, May 2021) it seems like the out parameters brings in content from the calling procedure. Should it be like this?
> Parameters in Ada are either passed by copy or passed by reference, regardless 
> of parameter mode. The rules are 
> 
> * Scalar types are always passed by copy 
> * Tagged types are always passed by reference 
> * Limited types are always passed by reference 
> * All other types are decided by the compiler 
> 
> For the types that are passed by reference, "in out" and "out" mode are identical. 
> 
> Vector is a tagged type, so this applies to it. 
> 
> One can argue that an out-mode parameter of a by-reference type should be 
> "reinitialized" before use, but the Ada-9X revision decided not to require this. 
> 
> -- 
> Jeff Carter 
> "Hello! Smelly English K...niggets." 
> Monty Python & the Holy Grail 
> 08

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Question on in/out parameters
  2022-04-30 11:30   ` reinert
@ 2022-05-01  7:50     ` J-P. Rosen
  0 siblings, 0 replies; 5+ messages in thread
From: J-P. Rosen @ 2022-05-01  7:50 UTC (permalink / raw)


Le 30/04/2022 à 13:30, reinert a écrit :
> So there is no difference between "in", "in out" and "out" for
> Vectors except that the compiler protests if I try to change an "in
> parameter" in the actual subroutine/function?
> 
"in" is read only, so there is a difference.

The difference between "in out" and "out" is not for the compiler (in 
this case), but for the reader: if you declare a parameter as "out", you 
promise that you won't use the previous value of the parameter, and 
therefore that the procedure is OK if you call it with an uninitialized 
variable (i.e. that the procedure is appropriate to initialize an 
otherwise uninitialized variable).

-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52
https://www.adalog.fr

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Question on in/out parameters
  2022-04-30  8:57 Question on in/out parameters reinert
  2022-04-30  9:38 ` Jeffrey R.Carter
@ 2022-05-02 21:35 ` Randy Brukardt
  1 sibling, 0 replies; 5+ messages in thread
From: Randy Brukardt @ 2022-05-02 21:35 UTC (permalink / raw)


"reinert" <reinkor@gmail.com> wrote in message 
news:85d12db3-e308-46bc-9be6-20b48ebe4fd2n@googlegroups.com...
>I expected an "out" parameter in a procedure to be like declaring the
>parameter "from scratch" (with actual initial default value). For my
>compiler (GNAT Community Edition, May 2021) it seems like the
>out parameters brings in content from the calling procedure. Should
>it be like this?

It depends on the data type; there is a complex series of rules about what 
is passed in for an out parameter. The basic idea is that stuff like bounds 
and discriminants are passed in, while other things may or may not be passed 
in.

Probably it is best to think of the distinction between in out and out 
parameters as being for the programmer, as noted by Jean-Pierre. When you 
say "out", you are asserting that you do not care what is passed in -- it 
could be in any state or even uninitialized. When you say "in out", you 
expect a value that you can use in the subprogram, so it has to meet the 
constraints and other requirements.

For instance, "out" parameters are checked as little as possible on the way 
in, while an "in out" parameter has all of the checking one might expect. Of 
course, if you are working with a well-designed ADT (as in the case of the 
containers), the difference between an unchecked item and a fully checked 
item is minimal. If you were working with an elementary type like an integer 
or access type, the input value may not be initialized at all.

Hope this helps.

                               Randy.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-05-02 21:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-30  8:57 Question on in/out parameters reinert
2022-04-30  9:38 ` Jeffrey R.Carter
2022-04-30 11:30   ` reinert
2022-05-01  7:50     ` J-P. Rosen
2022-05-02 21:35 ` Randy Brukardt

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