tags:

views:

139

answers:

2

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?

+2  A: 

An in parameter can be read but not written by the procedure. in is the default. Functions are 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;
Simon Wright
@Simon...Can you please make this statement more clear plz "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"
maddy
@Maddy... better?
Simon Wright
@Simon...That is more clear and precise.Thanks a lot.
maddy
Juast as an FYI: The next revision of Ada, Ada 2012, will no longer restrict function arguments to in and access, i.e. functions will be allowed to have out and in out parameters as well. http://www.ada-auth.org/cgi-bin/cvsweb.cgi/ai05s/ai05-0143-1.txt?rev=1.3
Marc C
+1  A: 

Basically, every parameter to a function or procedure has a direction to it. The options are in, out, in out (both), or access. If you don't see one of those, then it defaults to in.

in means data can go into the subroutine from the caller (via the parameter). You are allowed to read from in parameters inside the routine. out means data can come out of the routine that way, and thus you are allowed to assign values to the parameter inside the routine. In general, how the compiler accomplishes the data passing is up to the compiler, which is in accord with Ada's general philosophy of allowing you to specify what you want done, not how you want it done.

access is a special case, and is roughly like putting a "*" in your parameter definition in Cish languages.

The next question folks usually have is "if I pass something large as an in parameter, is it going to push all that data on the stack or something?" The answer is "no", unless your compiler writers are unconsionably stupid. Every Ada compiler I know of under the hood passes objects larger than fit in a machine register by reference. It is the compiler, not the details of your parameter passing mechanisim, that enforces not writing data back out of the routine. Again, you tell Ada what you want done, it figures out the most efficient way to do it.

T.E.D.
@T.E.D...Thanks a lot for that imformation
maddy