A function call is a subprogram of
the form of an expression that returns a value.
(parameters) return type;
(parameters) return type is
end function function_name;
The function is a subprogram that either defines an algorithm for
computing values or describes a behavior. The important feature of
functions is that they are used as expressions that return values of
specified type. This is the main difference from another type of
which are used as statements.
The result returned by a function can be of either scalar or complex type.
Functions can be either pure
(which is default) or impure.
Pure functions always return the same value for the same set of
actual parameters. Impure functions may return different values for
the same set of parameters. Additionally, an impure function may have
side effects, like updating objects outside of their
scope, which is not allowed for pure functions.
The function definition consists of two parts:
function declaration, which
consists of name, parameter list and type of the values returned by
function body, which
contains local declarations of nested subprograms, types, constants,
variables, files, aliases, attributes and groups, as well as sequence
of statements specifying the algorithm performed by the function.
The function declaration is
optional and function body,
which contains a copy of it, is sufficient for correct specification.
However, if a function declaration
exists, the function body declaration
must appear in the given scope.
The function declaration can be preceded by an optional reserved word pure
or impure, denoting the
character of the function. If the reserved word is omitted it is
assumed by default that the function is pure.
The function name, which appears after the reserved word function, can
be either an identifier or an operator symbol (if the function
specifies the operator). Specification of new functions for existing
operators is allowed in VHDL and is called operator
overloading. See respective topic for details.
The parameters of the function are by definition inputs and therefore
they do not need to have the mode (direction) explicitly specified.
Only constants, signals and files can be function parameters. The
object class is specified by a reserved word (constant,
signal or file,
respectively) preceding the parameter's name. If no reserved word is
used, it is assumed by default that the parameter is a constant.
In case of signal parameters the attributes of the signal are passed
into the function, except for 'STABLE, 'QUIET, 'TRANSACTION and
'DELAYED, which may not be accessed within the function.
If a file parameter is used, it is necessary to specify the type of
the data appearing in the opened file.
Example 1 contains several examples of function declarations.
Function body contains a sequence of statements that specify the
algorithm to be realized within the function. When the function is
called, the sequence of statements is executed.
A function body consists of two parts: declarations and sequential
statements. At the end of the function body, the reserved word end
can be followed by an optional reserved word function
and the function name. Examples 2 through 4 illustrate the function bodies.
If a function is explicitly specified as an impure (which is denoted
with the reserved word impure, preceding
the function declaration) it may return different results in
different calls even with the same parameters. See Example 5.
type Int_Data is
file of NATURAL;
function Func_1 (A,B,X:
REAL) return REAL;
function "*" (a,b:
Integer_new) return Integer_new;
function Add_Signals (signal
In1,In2: REAL) return REAL;
function End_Of_File (file
File_name: Int_Data) return BOOLEAN;
The first function above is called Func_1, it has three parameters A,
B and X, all of REAL type and returns a value also of REAL type.
· The second function defines a new algorithm for executing
multiplication. Note that the operator is enclosed in double quotes
and plays the role of the function name.
· The third function is based on signals as input parameters,
which is denoted by the reserved word signal preceding the parameters.
· The fourth function declaration is a part of the function
checking for end of file, consisting of natural numbers. Note that
the parameter list uses the Boolean type declaration.
function Transcod_1(Value: in
bit_vector(0 to 7)) return
case Value is
"00000000" => return "01010101";
"01010101" => return "00000000";
=> return "11111111";
The case statement has been used to realize the function algorithm.
The formal parameter appearing in the declaration part is the Value
constant, which is a parameter of the Bit_vector type. This function
returns a value of the same type.
function Func_3 (constant
A,B,X: real) return real is
The formal parameters: A, B and X are constants of the real type. The
value returned by this function is a result of calculating the
A*X**2+B expression and it is also of the real type.
function Func_4 (constant
real) return real is
variable counter, max, temp: real;
counter <= RightB loop
temp > max then
:= counter + Step;
end loop L1;
The fourth example is much more complicated. It calculates the
maximum value of the Func_1 function.
All the formal parameters are constants of the real type. When the
function is called, the A and B values appearing in the function are
passed, Step is a determinant of calculation correctness. The LeftB
and RightB values define the range in which we search for the maximum
value of the function.
Inside the function body are contained definitions of variables
counter, max and temp. They are used in the simple algorithm, which
calculates all the function values in a given range and storing the
maximum value returned by the function.
variable number: Integer := 0;
Func_5 (A: Integer) return
variable counter: Integer;
A * number;
number + 1;
Func_ 5 is an impure function; its formal parameter A
and returned value are constants of the integer type. When the
function is invoked, output value depends on the variable number
declared outside the function.
The number variable is additionally updated after each function call
(it increases its value by 1). This variable affects the value
calculated by the function, that is why the out function value is
different for the same actual parameter value.
Functions can be called recursively.
Function body may not contain a wait statement or a signal assignment.
Subprograms (functions and procedures) can be nested.