Functions
Declaring a function
Every function declaration has the form
<access> <return type> <name>(<parameters>) { <body> }
where <access> is public, private, or protected. It’s required,
as is the return type (void when there is no result). There is no
function keyword.
package main;
public int add(int a, int b) {
return a + b;
}
private void greet(string name) {
trace("hello", name);
}
protected int main() {
greet("rune");
trace(add(2, 3));
return 0;
}
| Access | Meaning |
|---|---|
public |
Usable from everywhere |
private |
Usable only in the same file |
protected |
Usable only in the same package |
These rules are enforced when a program has several files; see Packages and files.
return; exits a void function early.
Calling
Arguments are checked against the parameter types using the same rules as
assignment. For example, you can’t pass an int variable to a uint
parameter. Top-level functions can be called before their definition:
package main;
protected int main() {
trace(square(12)); // defined below: fine
return 0;
}
public int square(int n) {
return n * n;
}
A call whose result you don’t need can be used as a statement:
package main;
public int logAndCount(string message) {
trace(message);
return 1;
}
protected int main() {
logAndCount("result ignored");
return 0;
}
Parameters are copies, and read-only
Arguments are passed by value. Inside the function, parameters can’t be
reassigned (ASX-NAME-006). Copy the value into a local variable if you
need to change it:
package main;
public uint gcd(uint a, uint b) {
uint x = a;
uint y = b;
while (y != 0) {
uint t = x % y;
x = y;
y = t;
}
return x;
}
protected int main() {
trace(gcd(84, 36)); // 12
return 0;
}
Recursion
package main;
public int factorial(int n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}
protected int main() {
trace(factorial(10)); // 3628800
return 0;
}
Calls can nest up to 10,000 deep. Going deeper stops the program with
ASX-RUN-001 instead of crashing.
Prototypes (forward declarations)
A prototype declares a function’s signature without its body. It ends
in ;, and parameter names are optional. The definition must come later
in the same scope, with exactly the same types:
package main;
public int twice(int); // prototype
protected int main() {
trace(twice(21)); // 42
return 0;
}
public int twice(int x) { // definition
return x * 2;
}
Top-level functions are visible everywhere anyway, so prototypes are
optional there. Inside a function they’re how two nested functions call
each other (see below). Declaring a prototype twice, or after the
definition, is an error (ASX-NAME-002). A definition with different types
is ASX-TYPE-015. A prototype that is never defined is ASX-NAME-010.
Nested functions
Functions can be declared inside other functions, with an access modifier like any other function. A nested function is visible from its declaration to the end of the enclosing block, and inside its own body (so it can be recursive):
package main;
protected int main() {
private int fib(int n) {
if (n < 2) {
return n;
}
return fib(n - 1) + fib(n - 2);
}
trace(fib(15)); // 610
return 0;
}
To make two nested functions call each other, declare one with a prototype first:
package main;
protected int main() {
private bool isEven(int n);
private bool isOdd(int n) {
if (n == 0) {
return false;
}
return isEven(n - 1);
}
private bool isEven(int n) {
if (n == 0) {
return true;
}
return isOdd(n - 1);
}
trace(isEven(10), isOdd(7)); // true true
return 0;
}
Function boundaries
Every function body, including nested functions and lambdas, is a boundary. Code inside it can use:
- its own parameters and local variables;
- functions: top-level functions, and nested functions visible where it is declared (including itself).
It cannot use the local variables or parameters of the function it’s
nested in. RuneScript functions don’t capture variables (ASX-NAME-009).
The one exception is runes in web components, which nested functions and
lambdas may use (chapter 15):
package main;
protected int main() {
int count = 0;
private void bump() {
count++; // error: ASX-NAME-009
}
return 0;
}
Pass the value in as a parameter, and return the result:
package main;
protected int main() {
private int bump(int count) {
return count + 1;
}
int count = 0;
count = bump(count);
trace(count); // 1
return 0;
}
Because of the boundary, a nested function can reuse a name from the
enclosing function. Inside the nested function, that name refers only to
the nested function’s own variable. break and continue can’t cross a
boundary either.
Function types
A function type is written C-style: the return type, followed by the parameter types in parentheses.
| Type | Meaning |
|---|---|
int(int, int) |
Takes two ints, returns an int |
void(string) |
Takes a string, returns nothing |
bool() |
Takes nothing, returns a bool |
int(int)(string) |
Takes a string, returns an int(int) function |
Function types can be used for variables, parameters, and return types.
Two function types are the same when their parameter and return types
are the same (int and sint count as the same type).
Functions as values (callbacks)
Naming a function without calling it gives a function value. You can store it, pass it, return it, and call it:
package main;
public int apply(int(int, int) op, int a, int b) {
return op(a, b);
}
public int multiply(int a, int b) {
return a * b;
}
protected int main() {
int(int, int) f = multiply;
trace(f(6, 7)); // 42
trace(apply(multiply, 3, 4)); // 12
return 0;
}
Function values can’t be traced or compared with ==. The built-in
trace isn’t a value; wrap it in a lambda instead.
Lambdas
A lambda is an anonymous function: typed parameters in parentheses,
then =>, then either an expression or a block.
package main;
public void repeat(int times, void(int) body) {
for (int i = 0; i < times; i++) {
body(i);
}
}
protected int main() {
int(int, int) add = (int a, int b) => a + b;
trace(add(2, 3)); // 5
repeat(2, (int i) => trace("tick", i)); // tick 0, tick 1
int(int) abs = (int x) => {
if (x < 0) {
return -x;
}
return x;
};
trace(abs(-4)); // 4
return 0;
}
- Return type. When a lambda is assigned, passed, or returned where a
function type is expected, it takes that type’s return type. Otherwise
the return type comes from the body: the expression’s type, or the type
of the first
return. - Lambdas follow the same boundary rule as nested functions: they can’t use the enclosing function’s variables.
- Lambdas don’t take an access modifier.
Returning functions
package main;
public int(int) pick(string op) {
private int double(int n) {
return n * 2;
}
if (op == "double") {
return double;
}
return (int n) => n * n;
}
protected int main() {
trace(pick("double")(5), pick("square")(5)); // 10 25
return 0;
}
pick("double")(5) calls the function returned by pick. The function
value is evaluated first, then the arguments.
Not yet available
Overloading (two functions with the same name), default arguments, variadic parameters, and closures that capture variables. Generic functions are left out on purpose (chapter 17).