Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The use of IN OUT in Ada

Tags:

ada

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?

like image 536
maddy Avatar asked Jun 09 '10 06:06

maddy


People also ask

What are the two types of subprograms?

Subprograms are named PL/SQL blocks that can take parameters and be invoked. PL/SQL has two types of subprograms called procedures and functions. Generally, you use a procedure to perform an action and a function to compute a value.

How do you call a procedure in Ada?

A procedure call in Ada constitutes a statement by itself. A_Test (5 + P, 48, Q); the expressions 5 + P and 48 are evaluated (expressions are only allowed for in parameters), and then assigned to the formal parameters A and B, which behave like constants.

How do you call a subprogram in Ada?

There are two kinds of subprograms in Ada, functions and procedures. The distinction between the two is that a function returns a value, and a procedure does not. In this example, we see that parameters can have default values. When calling the subprogram, you can then omit parameters if they have a default value.

What is Ada compiler?

Ada improves code safety and maintainability by using the compiler to find errors in favor of runtime errors. Ada is an international technical standard, jointly defined by the International Organization for Standardization (ISO), and the International Electrotechnical Commission (IEC).


2 Answers

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;
like image 131
Simon Wright Avatar answered Oct 31 '22 09:10

Simon Wright


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.

like image 40
T.E.D. Avatar answered Oct 31 '22 07:10

T.E.D.