Vǫrðr Forge Steam page

Rûn Language

Rûn Functions

A function is a named piece of a program that you call from several places. Event scripts have no functions; their nearest thing is a section that other scripts run (see Rûn Statements and Control Flow).

Defining and calling

A function is written at the top level of a program, never inside another block: its name, its parameters in brackets, each with a type, and its body in braces. return gives a value back.

Damage(number base, number bonus) {
    return base * 2 + bonus
}
print(Damage(5, 1))
print(Damage(Damage(1, 0), 0))
11
4

A program is read whole before it runs, so a function may be called above the place where it is written.

print(Twice(21))
Twice(number n) {
    return n + n
}
42

Parameters

A function takes at most eight parameters. The types are number, text, toggle, a struct your project declares, or an engine type. A call must give exactly as many arguments as the function has parameters.

Pair(number a, number b) {
    return a + b
}
print(Pair(1))
Pair wants 2 arguments, got 1

A parameter typed with a declared struct takes only that struct or one built on it. A parameter typed number, text or toggle takes whatever it is given; the type is a note to the reader.

Twice(number n) {
    return n + n
}
print(Twice("ab"))
abab

Return values

return value ends the function and gives the value back. return alone, or reaching the end, gives back nothing, which prints as none and counts as false.

Greet(text who) {
    print("hello, " + who)
}
number got = 0
got = Greet("Ada")
print(got)
hello, Ada
none

Variables inside a function

A variable a function makes, its parameters included, is gone when the function returns. While the function runs it can also read and change the variables of the code that called it: a name is looked for in the function first, then in its caller, and so on outward.

Bump() {
    hits = hits + 1
}
number hits = 0
Bump()
Bump()
print(hits)
2

Because of this, a declaration inside a function with the same name as one of the caller's variables changes the caller's variable rather than making a new one. Give a function's own variables names the callers do not use. See Rûn Variables and Scope.

Recursion

A function may call itself. Calls nest at most 16 deep, counting every function on the way.

Fact(number n) {
    if (n <= 1) return 1
    return n * Fact(n - 1)
}
print(Fact(10))
3628800
Fact(number n) {
    if (n <= 1) return 1
    return n * Fact(n - 1)
}
print(Fact(17))
functions are calling themselves too deep

Names

A function's name is matched without regard to case. A function of your own named like one of the math functions (round, min, clamp ...) is used instead of the built-in one. Some built-in names are tried before your own functions, so a function of your own by one of those names is never called. Because names ignore case, this catches natural names such as Roll, Dash or Movement:

Roll(number sides) {
    return 1
}
print(Roll(6))
6

Roll(6) called the built-in roll("6"), which rolled the dice expression 6, a flat 6. Name it RollDie and it is yours. The reserved names are random, pick, refuse, print, roll, ap, walkleft, actions, bonusactions, movement, useaction, usebonusaction, usemovement, dash, endturn, pathcost, gridmode, cellsize and addmodifier.

Functions the engine calls

Two engine features call a function of a program by script.Function: a random event's outcome of kind rune, and the companions' experience rule when it names a hook. Such a function is handed, in order, as many of an id (text), an amount (number) and a toggle as it declares, and must return a number. See Events and Triggers and Companions.

Sharing between scripts

A function belongs to its file. Another program cannot call it; it can run one of the file's sections with World.Fire("<script>", "<section>"). An event script runs them with run <script> <section> when no event script has the same name. Put functions that several sections need in the same file as those sections.