Dodo doc > Variables and Functions > Functions


Functions

Syntax

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
_閉じる
Rationale: variables and function identifiers share the same syntax because both must be distinguished from types.

Declaration

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.

Rationale: this gives the compiler the minimum information needed to use the function in other modules. The declaration can be documented (see section on comments) to provide more details.

Definition

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[]").

Rationale: the type before the first parameter helps telling the difference between function declaration and function definition. The first syntax looks like a variable definition with the function parameters added. The second syntax puts the function instructions in a block which makes it familiar to people coming from other languages. The continuation name is "return" to match with the return keyword. There is also an implicit "throw" continuation handled automatically by dodo.

Invocation

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)
Rationale: function invocation is written as expected in many programming languages.
A function call without arguments is equivalent to taking the value of a constant (see section on constants) so dodo does not require to type empty brackets.

^ 3.3. Linked References

v 3.5. Continuations