Functions
IPL provides a mechanism for declaring functions which help to organise code. In this section we describe how to declare and exploit functions in IPL.
Defining Functions
In order to define a function in IPL we write for example
Here we have explicitly given types to the arguments of the function max
we have defined, as well as an explicit return type. Every function must declare a return
statement for every branch. IPL will produce a validation error if this is not present. For example, defining the following function:
will result in the validation error:
This function does not specify a return value for all branches.
The types that can be defined on functions can also be general map, set or list types. For example the following is valid:
For Loops
It is possible to define simple for loops in ipl, either over a range of integer type or over the values of an enumeration type. To enumerate over an enumeration type it is possible to write for example:
To write a for loop for a range, one can declare a loop for example in a function:
where in for(0,count,2)
the 0
is the start counter, the expression count
is the end counter and the 2
is the step increment.
Calling Functions
A function can be called from any expression in the language. Specifically they can be called on validation statements, receive or reject code or other functions. As an example let us consider writing functions we can use in an action validator:
The validator calls the function we defined called max
.
We can also call functions from receive statements or other functions:
Note that it is not possible to define recursive functions since termination cannot be guaranteed in the generated code. For example defining the functions
will result in the error.
This function has a mutually recursive dependency which is not permissible in ipl. g->f->g
Referencing State
It is possible to reference an internal state declaration in a function, but it is not possible to modify its fields. For example writing the following function for our example above:
is admissible, but writing the function
will result in the error
Reassignment of internal state type fields is not allowed in functions.