The use of IN OUT in Ada

ada
maddy picture maddy · Jun 9, 2010 · Viewed 15.9k times · Source

Given below is some code in ada

  with TYPE_VECT_B; use TYPE_VECT_B;

  Package TEST01 is
  procedure TEST01
           ( In_State   : IN     VECT_B ;
             Out_State  : IN OUT VECT_B );

  function TEST02
           ( In_State   : IN     VECT_B ) return Boolean ;

  end TEST01;

The TYPE_VECT_B package specification and body is also defined below

  Package TYPE_VECT_B is

  type VECT_B is array (INTEGER  range <>) OF BOOLEAN  ;

  rounded_data : float ;
  count : integer ;
  trace : integer ;
  end TYPE_VECT_B;

  Package BODY TYPE_VECT_B is
  begin
   null;
 end TYPE_VECT_B;

What does the variable In_State and Out_State actually mean? I think In_State means input variable. I just get confused to what actually Out_State means?

Answer

Simon Wright picture Simon Wright · Jun 9, 2010

An in parameter can be read but not written by the subprogram. in is the default. Prior to Ada 2012, functions were only allowed to have in parameters. The actual parameter is an expression.

An out parameter implies that the previous value is of no interest. The subprogram is expected to write to the parameter. After writing to the parameter, the subprogram can read back what it has written. On exit the actual parameter receives the value written to it (there are complications in this area!). The actual parameter must be a variable.

An in out parameter is like an out parameter except that the previous value is of interest and can be read by the subprogram before assignment. For example,

procedure Add (V : Integer; To : in out Integer; Limited_To : Integer)
is
begin
   --  Check that the result wont be too large. This involves reading
   --  the initial value of the 'in out' parameter To, which would be
   --  wrong if To was a mere 'out' parameter (it would be
   --  uninitialized).
   if To + V > Limited_To then
      To := Limited_To;
   else
      To := To + V;
   end if;
end Add;