Functions
Top-level functions use the def keyword. They may declare typed parameters,
default values, overloads, recursive behavior, explicit return types, and exception contracts.
def add(a as Int, b as Int) as Int:
return a + b
def greet(name as String = "World") as String:
return "Hello, " + name
def add(a as Int, b as Int) as Int:
return a + b
def add(a as Double, b as Double) as Double:
return a + b
Overloads are resolved by parameter types, so explicit typing is especially important here.
Place ... after the parameter name when a function accepts zero or more values of
one statically known type. The function body receives those values as an
ArrayList<T>. A variadic parameter must declare its element type and cannot have
a default value.
def sumAll(values... as Int) as Int:
total as Int = 0
for value in values:
total += value
return total
assert sumAll() == 0
assert sumAll(10, 20, 30) == 60
def powers(n as Int):
return n, n ** 2, n ** 3
result = powers(10)
a, b, c = powers(10)
Returning multiple comma-separated values produces a positional
Tuple<...Ts> that can be stored or unpacked.
def fact(n as ULong) as ULong:
return 1 if n == 0 else n * fact(n - 1)
Functions may raise exceptions as part of their normal implementation. Public APIs on types also
commonly declare explicit throws clauses. The detailed exception model is covered in
Exceptions and Resource Safety.
Top-level functions use def. Methods declared inside a class body do not. Inside
a class you write declarations such as public compute() as Int: directly.