Sieve subroutines

(New in MS 8.0.) Support for user-defined routines has been added to the recipe language, and to system-level Sieves. Subroutine definitions have the general form: sub routine-name {routine-body} or if parameters are needed: sub routine-name(parameter1, parameter2, ...) {routine-body} The " " control command can be used to return a specified result from the routine.

Parameters are passed by value and evaluation of parameters is lazy. If a parameter is never referenced it will never be evaluated. Note that evaluation of parameters in a particular order can be forced very easily: sub f(p1,p2,p3) { p1; p2; p3; ... } Local variables can be declared in a routine by specifying the " " control command immediately preceeding the first use of the variable.

Autoincrement, autodecrement, and the various augmented assignment operators are all allowed on parameters and local variables. So is the exchange operator. However, exchange cannot be used with a global variable on the right hand side and a local variable or parameter on the left hand side.

For example, a factorial function can be defined as follows: sub f(n) {if n &#x3c;= 1 {return 1;} else {return f(n-1)&#x2a;n;}} Recursion is limited to 20 levels. A routine can only call itself recursively since there is currently no forward declaration mechanism.

An example of the use of " " would be: sub fib(n) {my s = &#x5b;1, 1&#x5d;; my a = 1; my b = 1; loop {exitif --n &#x3c; 2; my c = a + b;                  s .= c;                   a = b;                   b = c;} return s;} Note that use of user-defined routines in Sieves is restricted to system-level Sieves.

See also:
 * Recipe language
 * Sieve hierarchy
 * Sieve variables extension
 * Sieve supported extensions