You can write your own functions in SPLASH. They can be declared in global blocks, for use by any stream or window, or within a local block to restrict usage to the local stream/window. A function can internally call other functions, or call themselves recursively.
type functionName(type1 arg1, ..., typen argn) { ... }
Each
"function type" is a SPLASH type, and each arg is the name of an argument. Within the {...}
can appear any SPLASH statements. The value returned by the function is the value returned by
the return statement within.
integer factorial(integer x) {
if (x <= 0) {
return 1;
} else {
return factorial(x-1) * x;
}
}
string odd(integer x) {
if (x = 1) {
return 'odd';
} else {
return even(x-1);
}
}
string even(integer x) {
if (x = 0) {
return 'even';
} else {
return odd(x-1);
}
}
integer sum(integer x, integer y) { return x+y; }
string getField([ integer k; | string data;] rec) { return rec.data;}
The first function is recursive. The second and third are mutually recursive; unlike C, you do
not need a prototype of the "even" function in order to declare the "odd" function. The last two functions illustrate multiple arguments and
record input.
float bondValue(float currentPrice,
integer daysToMature,
float inflation)
{
...
}