Vult has two types of functions: passive and active. Passive functions are like the examples that we saw in the Vult Language Basics, for example:
These functions simply receive arguments, perform computations and return a value.
One of the most useful features of Vult are the active functions. These functions have the characteristic that have internal and independent memory variables. The memory variables are declared with the keyword mem
. For example, if we would like to make a counter in Vult we could write the following function:
In the code shown above, let’s assume that an external function calls repeatedly the function loop
. Every time the function loop
is called, the function counter
is called and increases the value of the memory variable count
.
One very important thing to note is that every call to an active function creates a new context.
For example, if we have the following code:
the first call to counter
creates it’s own count
variable and is not affected by the second call. So the values of first
will be 1, 2, 3 ...
while the values of second
will be 10, 20, 30, ...
Another example of using functions with memory is the following:
This function returns true
every n
times is called. This can be used to limit the number of times an expensive computation is performed. For example:
Other function that can be very useful is:
In this case, the function returns true
every time it’s value changes compared to its previous value.
In the following tutorial we will see more advanced use cases focusing on a DSP application.