Dodo doc > Variables and Functions > Functions
Function names must start with a lower case letter and can contain letters, digits, underscore and apostrophe.
Alternatively the function name can be preceded with an underscore which allows more characters. In that case the first character must be a non-uppercase letter as defined by the Unicode standard.
Function names use the same syntax as variable names.
Examples:
repaint
_閉じる
A function can be declared separately from its implementation, for example when it appears in a header file. A function declaration starts with "def" in front of the function name followed by the type of the parameters inside brackets, an arrow and the return type.
Examples:
def distanceFromOrigin(Double, Double) -> Double
def format(String, ...Object) -> String
An optional parameter is marked with "?" and must not be followed by a non-optional parameter. The last parameter can be marked with "...", in which case it is a variable length list parameter which can accept zero or more arguments. Since the argument list can be empty this parameter behaves like an optional parameter.
It is possible to specify an alternative return type after the first but it must be handled specially. An example is the equality operator "=" which has two separate continuations.
A function definition is where you define what the function does. It must name the function parameters and provide an implementation.
The implementation can be an expression preceded with "=" or it can be a block containing instructions.
Examples:
def distanceFromOrigin(_ x, y) =
((x - x0)**2 + (y - y0)**2) ** 0.5
def format(String formatString, ...Object values) -> return(String):
return foreach(pattern = formatString, value in values) format_one(pattern, value) .
The type of parameters needs not be repeated if it is the same for successive parameters. It can be placeholder "_" for local type inference. The return continuation is optional in the definition; it is written "-> return" followed by the return type.
Optional parameters must have a default value defined with "=" followed by the value. The type of a variable length list parameter is a list (in this example "Object[]").
A function call takes arguments and returns a value.
Example:
distanceFromOrigin(-2.45e2, 1.03e3)
If there are no arguments the empty brackets can be left out. If the function has a variable length list parameter any number of arguments can be passed in its place.
Examples:
step
format("Point: (%g, %g) Distance: %g\n", x, y, d)