A function differs from a procedure in that a function produces a result, a temporary data item (see Section 5.1). This result
is obtained during elaboration of the body by elaborating a return statement which specifies the value of the result to be
produced. A function is elaborated when a function invocation occurs as an operand in an expression.
RULES
Identifier or definable symbol 1 is defined to be a function in the scope in which the function declaration appears.
Identifier or definable symbol 2 must be the same as identifier or definable symbol 1.
The type or subtype following => is known as the result type or subtype. The result type or the type
of the result subtype must be assignable.
Elaboration of a function invocation primary proceeds in the following order:
- Actual parameters are elaborated;
- actual parameters are bound to the formal parameters of the named function (see Section 7.3);
- the body of the function is elaborated until a return statement is encountered;
- a result temporary data item is created, whose subtype is the result subtype (if specified) or
the subtype of the expression in the return statement (if a result type is specified); and
- the result of the function is produced by assigning (via :=) the value of the expression in the
return statement to a result temporary data item. The := definition used is the one known in the scope
where the function is defined.
There must be no way to reach the point immediately after the last statement in the body of the function,
i.e., elaboration must always complete by the use of a return statement.
Additional rules for the return statement are given in Section 6.7, and (for side-effects and normality of
functions) in Section 7.2.1.
NOTES
A function declaration is a closed scope (see Section 3.5); the formal parameter names and
the result variable are defined in this scope.
The result subtype may depend on the subtypes and values of the actual parameters.
Functions may be overloaded and may be generic. Overloading, generics, and use of the translation time
property list are discussed in Section 11.
Use of definable symbol names is discussed in 13.2.
The order in which actual parameters are elaborated and bound is unspecified.
EXAMPLES
7.2.1 DECLARING AND USING A NEW TYPE
There are two kinds of functions: normal and abnormal functions.
The result of the invocation of
a normal function depends only upon the values of the actual parameters. Several invocations of a
normal function with the same actual parameter values may be replaced by the translator with a single
invocation (this is called common subexpression elimination). The result of the invocation of an
abnormal function can depend upon the values of variables other than those in the actual parameters.
All abnormal functions should be preceded by the reserved word ABNORMAL. Both abbreviations and
types are assumed to be normal; the result of invoking an abbreviation or type should depend only
upon the values of the actual parameters.
A function is said to have a side-effect if the function modifies any data whose lifetime is longer
than the function invocation. Programs are more understandable, reliable, and verifiable when
functions have no side-effects. However, there are cases where having side-effects is useful. The
language allows normal functions to have side-effects providing these side-effects are restricted to
modifications of data items that are local to the body of a capsule in which the function is also local.
For normal functions with side-effects, the user must ensure that any common subexpression
elimination will not have an undesired effect upon program behavior.
RULES
The order of elaboration within expressions is not defined. This means that the order in which
slde-effects occur within expressions is not guaranteed.
If more than one exception could be raised while elaborating an expression, which of these
exceptions is actually raised is not defined.
If there are several invocations anywhere in a program of a normal function, a type, or an
abbreviation whose corresponding actual parameters have the same value, these invocations may be
replaced (by a translator) by a single invocation which occurs at the point of the first of these
invocations.
A normal function may have only CONST and READONLY formal parameters.
If a normal function has an imports list, then:
- the function must be local to a capsule body;
- variables imported by the function must be defined locally in the capsule body in which the
function is local;
- no invocations of the function may appear anywhere within the capsule in which the function ls
defined; and
- no variables imported by a normal function may be exported from the capsule.
NOTES
Failure to mark an abnormal function, or the presence of an abnormal abbreviation or type, means that common subexpression
elimination may produce undesired results.
The only normal functions which have no parameters are functions which always produce the same constant value.
EXAMPLES
- a normal function with no side-effects.
- A normal function, fact, with a restricted side-effect which records the number of times it is
called. This count is returned by an abnormal fuction, factcnt.
- A normal function with a restricted side-effect -- a memo function.
- An abnormal random number generator function.
- A symbol table.