Functions

Define reusable code with `fun`.

Top-level functions use the fun keyword followed by a name: fun greet(name) { ... }. Parameters are comma-separated. Use return to send a value back.

Rain also supports **anonymous functions**: fun(params) { ... } without a name, as an expression. Pass them directly to other functions or store them in a variable. See **Anonymous functions**.

For functions that accept a variable number of arguments, put ...name as the **last** parameter. See **Variadic functions**.

Calling a fixed-arity function with the wrong number of arguments is a runtime error.

Define and call

Declare a named function with `fun`, then call it like any other value.

Define and call
fun greet(name) {
  print "Hello, " + name + "!";
}

greet("Rain");
output
Hello, Rain!

Return values

Use `return` to send a result back to the caller.

Return values
fun add(a, b) {
  return a + b;
}

print add(2, 40);
output
42

Expression as statement

Calling a function without using its result is fine: the return value is discarded.

Expression as statement
fun double(x) { return x * 2; }
double(5);