Skip to content

Functions

Functions represent a basic way of grouping TML code. Functions contain statements, expressions and can return a value. TML supports three types of functions: entry, custom and built-in.

Entry functions

Entry functions represent concrete steps in a simulation cycle. Entry functions can be called only by the compiler and calling them on arbitrary places in user code is not allowed. When the model is compiled, the order of entry function execution is chosen based on model graph.

Entry functions do not have parameters and do not have a return value. Using return statement in entry functions is not allowed.

Following table shows supported entry functions and their usage:

Function Description
init_fnc Initialize component states before simulation is started.
output_fnc Update component outputs for the current simulation cycle.
update_fnc Update component states for the current simulation cycle.

Note

Entry functions are similar to main function present in nearly all programming languages. The difference is that there are more entry functions, that some are optional and that entry functions from all components are called in a simulation cycle. In contrast, only one instance of main is called in standard program execution.

Note

If init_fnc contains terminal accesses that are reachable under current component configuration, initialization is treated as dynamic. Dynamic initialization functions are not ran alongside static initialization function before the simulation is started. Dynamic initialization functions are ran once before the first iteration of component's output_fnc.

Custom functions

Custom functions are functions defined by the user. They can have an arbitrary number of parameters and an arbitrary return type. Defining custom functions is not mandatory. Custom functions can be called from entry functions, other custom functions and from global scope.

Variables that a custom function is allowed to use depend on the context from which it is called. If called from an entry function (either directly or indirectly via a call chain), custom function has the same access rights as the parent. If called from the global scope, custom function must be pure (i.e. have no side effects).

Built-in functions

Built-in functions represent a basic standard library of mathematical functions. Builtin functions are pure and can be called from arbitrary places in code.

In contrast to custom functions, built-in functions are polymorphic. Return type is calculated based on the input type. For more details about supported built-in functions, see Built-in functions and Reduction.

Built-in reduction functions support two modes of reduction: innermost and overlapping. Innermost is the default reduction mode. Overlapping reductions are called using alternative built-in syntax func!(arg).

a = [1, 2, 3]

b = min(a)  # innermost reduction
c = min!(a) # overlapping reduction

Function declaration

The most general form of the function declaration:

fn <function_name> (<parameters_list>?) <return_type>?:
    <function_body>
end

When defining a function, one must define the type of its return value, if the function actually returns some value. Otherwise, the return type is omitted. Also, type of every function parameter must be provided. Parameters are passed by value, and writing to them in the function body does not affect their original values.

Declaration of a function without parameters and with no return values:

fn output():
    if p.value > 1:
        t.out = 1
    else:
        t.out = p.value
    end
end

Declaration of a function with two parameters. Note that declaring the types of parameters is mandatory.

fn _helper_function(real state, real mul_factor):
    state = 2 * mul_factor
end

In the previous example, return statement is optional, because the function does not return a value. However, return statement is mandatory if the return type of function is specified as in the following example.

fn foo(int arg) int:
    if arg > 10:
        return 55
    else:
        return 5
    end
end

Warning

Compiler will check if all possible execution paths in function have a return statement, and return an error if necessary. Whole function is checked, even pruned code sections.

Important

Custom defined functions do not support overloading. Builtin functions support different types through simple builtin polymorphism, and separate function implementations are generated for different types by the compiler.

Warning

At the moment, TML does not support:

  • default argument values
  • optional arguments