Custom Functions

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.

The syntax of SPLASH functions resembles C. In general, a function looks like:
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.
Here are some examples:
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.
The real use of SPLASH functions is to define, and debug, a computation once. Suppose, for instance, you have a way to compute the value of a bond based on its current price, its days to maturity, and forward projections of inflation. You might write this function and use it in many places within the project:
float bondValue(float currentPrice,
                 integer daysToMature,
                 float inflation) 
{
  ...
}