Dodo doc > Variables and Functions > Functions


Functions

Syntax

Function names use the same syntax as variable names. They must start with a lower case letter and can contain letters, digits, underscore and apostrophe.

Alternatively the function name can be enclosed in single quotes which allows more characters. In that case the first character must be a non-uppercase letter as defined by the Unicode standard.

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 follows the same pattern as variable declaration except that the type of the parameters is listed after the function name.

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 and can replace it.

Examples:

Double distanceFromOrigin(Double, Double)
String format(String, Object*)
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 information.

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

String format(String formatting, Object[] values):
    return format_one(_, values[each]) ?: formatting.

The type of parameters needs not be repeated if it is the same for successive parameters. It can be keyword "def" for local type inference.

Optional parameters must have a default value defined. The type of a variable length list parameter must be a list type (e.g. "Object[]"). Note that a variable length list parameter cannot be declared in the function definition, it must be done in its declaration.

For a function declared with an initialisation value, the return type can be "def" for local type inference in which case the type of the parameters is not required.

Rationale: as variables and functions are closely related in dodo, the value they return can be defined using the same syntax - the only difference is that functions have parameters. The block syntax is similar to C and Java, providing a familiar form. The requirement for optional parameters default value to be provided is to ensure the function declaration matches with its definition.
Since whenever the return type is "def" it is a function definition with an initialisation value, the compiler can not confuse its parameter names with parameter types in a function declaration. So parameter types are made optional when the return type is "def".

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:

format("Point: (%g, %g) Distance: %g\n", x, y, d)
step
Rationale: 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 4. Types