Function
Complete Definition:
A function call is a subprogram of
the form of an expression that returns a value.
Simplified Syntax
function function_name
(parameters) return type;
function function_name
(parameters) return type is
declarations
begin
sequential statements
end function function_name;
Description
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
subprograms: procedures,
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
the function;

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.
FUNCTION DECLARATION
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
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.
IMPURE FUNCTIONS
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.
Examples
Example 1
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.
Example 2
function Transcod_1(Value: in
bit_vector(0 to 7)) return
bit_vector is
begin
case Value is
when
"00000000" => return "01010101";
when
"01010101" => return "00000000";
when others
=> return "11111111";
end case;
end Transcod_1;
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.
Example 3
function Func_3 (constant
A,B,X: real) return real is
begin
return A*X**2+B;
end Func_3;
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.
Example 4
function Func_4 (constant
A,B,Step,LeftB,RightB: in
real) return real is
variable counter, max, temp: real;
begin
counter:= LeftB;
max:=Func_3(A,B,
counter);
L1:
while
counter <= RightB loop
temp:=Func_1(A,B,
counter);
if
temp > max then
max:=temp;
end if;
counter
:= counter + Step;
end loop L1;
return max;
end Func_4;
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.
Example 5
variable number: Integer := 0;
impure function
Func_5 (A: Integer) return
Integer is
variable counter: Integer;
begin
counter :=
A * number;
number :=
number + 1;
return counter;
end Func_5;
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.
Important Notes

Functions can be called recursively.

Function body may not contain a wait statement or a signal assignment.

Subprograms (functions and procedures) can be nested.
