Subprograms and Packages 111
IF S(i) = ‘ 1 ’ THEN --line 9
result := result + 1; --line 10
END IF;
END LOOP;
RETURN result; --line 11
END vector_to_int;
BEGIN
O1 <= vector_to_int(I1); --line 12
END behave;
Line 1 of the example declares the array type used throughout the
example. Lines 2 and 3 show the input and output ports of the convert
entity and their types. Lines 4 through 11 describe a function that is
declared in the declaration region of the architecture behave. By declaring
the function in the declaration region of the architecture, the function is
visible to any region of the architecture.
Lines 4 and 5 declare the name of the function, the arguments to the
function, and the type that the function returns. In line 6, a variable local
to the function is declared. Functions have declaration regions very similar
to process statements. Variables, constants, and types can be declared, but
no signals.
Lines 7 through 10 declare a loop statement that loops once for each
value in the array type. The basic algorithm of the function is to do a shift
and add for each bit position in the array. The result is first shifted (by
multiplying by 2), and then, if the bit position is a logical 1, a 1 value is
added to the result.
At the end of the loop statement, variable resultcontains the integer
value of the array passed in. The value of the function is passed back via
the RETURNstatement. An example RETURNstatement is shown in line 11.
Finally, line 12 shows how a function is called. The name of the function
is followed by its arguments enclosed in parentheses. The function always
returns a value; therefore, the calling process, concurrent statement, and
so on must have a place for the function to return the value to. In this
example, the output of the function is assigned to an output port.
Parameters to a function are always input only. No assignment can be
done to any of the parameters of the function. In the preceding example, the
parameters were of a constant kind because no explicit kind was specified
and the default is constant. The arguments are treated as if they were
constants declared in the declaration area of the function.
The other kind of parameter that a function can have is a signal para-
meter. With a signal parameter, the attributes (which are discussed in
Chapter 6,“Predefined Attributes”) of the signal are passed in and are