Function

LRM §2.1, §2.2.

A function is a subprogram to group sequential statements and returns a value.

Syntax:

[ kind ] function function_name [ ( parameter_declaration; ) ] return type_name;     -- declaration
[ kind ] function function_name [ ( parameter_declaration; ) ] return type_name is
  [ function_declarations ]
begin
  sequential_statements
end [ function ] [ function_name ];                                                  -- body 

kind = pure | impure

Description:

The function is used to group together executable, sequential statements to define new mathematical or logical functions. The important feature of functions is that they are used as expressions that return values of a specified type. This is the main difference from another type of subprograms: procedures, which are used as statements.

Functions can be of type 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, and may have "side effects", like updating objects outside of their scope, this is not allowed for pure functions.

A function declaration consists of the name, parameter declaration and type of the value returned by the function.

A function body defines 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 the 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 name can be either an identifier or an operator symbol (if the function specifies the operator). VHDL allows the specification of new functions for existing operators this is called operator overloading.

The parameters of the function are by definition inputs and thus do not need to have the mode (direction) specified in the function declaration. Only constants, signals and files can be function parameters.

The function body contains the functional description of the function. A function body consists of two parts: declarations and sequential statements. A function body must execute a return statement.

Example:

function Func1 (signal A, B: in integer; MaxValue: integer) return integer is
  variable Temp, Max: integer;
begin
  Temp:= A + B;
  if Temp > MaxValue then
    Max:= MaxValue;
  else
    Max:= Temp;
  end if;
  return Max;
end Func1;

Notes:

See also:

Function call, Operator, Procedure, Return, Package, Type conversion