• Question on in/out parameters

    From reinert@21:1/5 to All on Sat Apr 30 01:57:37 2022
    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;

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jeffrey R.Carter@21:1/5 to reinert on Sat Apr 30 11:38:06 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From reinert@21:1/5 to All on Sat Apr 30 04:30:14 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From J-P. Rosen@21:1/5 to All on Sun May 1 09:50:03 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Randy Brukardt@21:1/5 to All on Mon May 2 16:35:29 2022
    "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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)