131
• Each input formal parameter of the function must be prefixed by the keyword input, and each result formal
parameter by the keyword output. All public variables are formal parameters.
• Input formal parameters are read-only after being bound to the actual arguments or default values, i.e., they
may not be assigned values in the body of the function.
• A function may not be used in connections, may have no equations, may have no initial algorithm, and can
have at most one algorithm section, which, if present, is the body of the function.
• A function may have zero or one external function interface, which, if present, is the external definition of
the function.
• For a function to be called in a simulation model, it must have either an algorithm section or an external
function interface as its body, and it may not be partial.
• A function cannot contain calls to the Modelica built-in operators
der, initial, terminal, sample,
Subtask.activated, Subtask.lastInterval, pre, edge, change, reinit, delay, cardinality,
inStream, actualStream, to the operators of the built-in package Connections, and is not allowed to
contain when-statements.
• The dimension sizes not declared with (
:) of each array result or array local variable [i.e., a non-input
components] of a function must be either given by the input formal parameters, or given by constant or
parameter expressions, or by expressions containing combinations of those (Section 12.4.3).
• The local variables of a function are not automatically initialized to the implicit default values of the data
type (Section 12.4.3) [(
e.g. 0.0 for Real) for performance reasons. It is the responsibility of the user to
provide explicit defaults or to define the values of such variables before they are referenced.]
• Components of a function will inside the function behave as though they had discrete-time variability.
Modelica functions have the following enhancements compared to a general Modelica
class:
• A function may be called using the conventional positional calling syntax for passing arguments.
• A function can be recursive.
• A formal parameter or local variable may be initialized through an assignment (
:=) of a default value in its
declaration. Initialization through an equation is not possible.
• A function is dynamically instantiated when it is called rather than being statically instantiated by an
instance declaration, which is the case for other kinds of classes.
• A function may have an external function interface specifier as its body.
• A function may have a return statement in its algorithm section body.
• A function allows dimension sizes declared with (
:) to be resized for non-input array variables, see Section
12.4.4.
12.3 Pure Modelica Functions
Modelica functions are pure, i.e., are side-effect free with respect to the Modelica state (the set of all Modelica
variables in a total simulation model), apart from the exceptional case specified further below. This means that:
• Modelica functions are mathematical functions, i.e. calls with the same input argument values always give
the same results.
• A Modelica function is side-effect free with respect to the internal Modelica simulation state. Specifically,
the ordering of function calls and the number of calls to a function shall not influence the simulation state.
[Comment 1: This property enables writing declarative specifications using Modelica. It also makes it possible for
Modelica compilers to freely perform algebraic manipulation of expressions containing function calls while still
preserving their semantics.]
[Comment 2: The Modelica translator is responsible for maintaining this property for pure non-external
functions. Regarding external functions, the external function implementor is responsible. Note that external
functions can have side-effects as long as they do not influence the internal Modelica simulation state, e.g.
caching variables for performance or printing trace output to a log file.]