134 Chapter Five
results from the procedure. Let’s examine what the result from the pro-
cedure is from the input array value shown here:
‘ 0 ’ ‘ 0 ’ ‘ 1 ’ ‘ 1 ’
The first step for the procedure is to initialize the output values to
known conditions, in case a zero length input argument is passed in.
Output argument x_flagis initialized to false and stays false until
proven otherwise.
The loop statement loops through the input vector zand progressively
adds each value of the vector until all values have been added. If the value
is a ‘ 1 ’, then it is added to the result. If the value is a ‘ 0 ’, then no addi-
tion is done. If any other value is found in the vector, the x_flagresult is
set true, indicating that an unknown condition was found on one of the
inputs. (Notice that parameter qis defined as an inout parameter. This is
needed because the value is read in the procedure.)
PROCEDURE WITH INOUT PARAMETERS The examples we have
discussed so far have dealt mostly with in and out parameters, but proce-
dures can have inout parameters also. The next example shows a procedure
that has an inout argument that is a record type. The record contains an
array of eight integers, along with a field used to hold the average of
all of the integers. The procedure calculates the average of the integer
values, writes the average in the average field of the record, and returns
the updated record:
PACKAGE intpack IS
TYPE bus_stat_vec IS ARRAY(0 to 7) OF INTEGER;
TYPE bus_stat_t IS
RECORD
bus_val: bus_stat_vec;
average_val : INTEGER;
END RECORD;
PROCEDURE bus_average( x : inout bus_stat_t );
END intpack;
PACKAGE BODY intpack IS
PROCEDURE bus_average( x : inout bus_stat_t ) IS
VARIABLE total : INTEGER := 0;
BEGIN
FOR i IN 0 TO 7 LOOP
total := total + x.bus_val(i);
END LOOP;
x.average_val := total / 8;