Subroutines
Subroutines are a way for the user to encapsulate a chunk of activity code and refer to it via a simple function call-like syntax. They may be defined via a subroutine_def statement, and thereafter, called just like any other function. A definition may name parameters. Each parameter is an ordinary variable of the activity, and a call assigns its arguments to the parameters, in order, before the body runs. A call passes exactly as many arguments as the definition names parameters. A subroutine may return a value: `return [expression]` as the last statement of its body (or the last statement of an if or else branch that ends the body), and a call of it may then be used as a value in an expression. The value is held in a variable of the activity named after the subroutine, so a call used as a value waits for the subroutine the way any read of a variable waits for its writer.
Runs on: either side: the compiler places the statement that calls it where the values it uses live.
Parameters are variables of the activity, not local to the subroutine: after the call each parameter still holds the argument it was given, two subroutines that name the same parameter share the variable, and a subroutine cannot call itself. A return statement is allowed only where nothing of the body follows it.
Format:
subroutine_def [name]([parameters]) { [statements] }
Examples
subroutine_def log_message_on_client(message) {
ifonclient {
JS< console.log(Env.message) >JS
}
}
log_message_on_client("hello")
subroutine_def twice(n) {
return n * 2
}
four = twice(2)