Functions
A function with two parameters a:A
and b:B
, and return type C
, is declared as:
function f(a:A, b:B) -> C {
c:C;
// do something
return c;
}
For a function without a return value, omit the ->
:
function f(a:A, b:B) {
// do something
}
function f() {
// do something
}
f(a, b)
f()
Generic functions
A function declaration may include type parameters that are provided arguments when the function is called. These are declared using angle brackets in the function declaration:
function f<T,U>(a:T, b:U) {
// do something
}
f<Real,Integer>(1.0, 2);
T
becomes Real
and U
becomes Integer
. In many cases it is unnecessary to explicitly provide these type arguments, however, as the compiler can deduce them itself. In this particular example, using f(1.0, 2)
is sufficient. Sometimes the compiler cannot deduce them itself, and they must be specified explicitly.