A Function is an expression which defines operations to be performed when it is sent the value
message. In functional languages, a function would be known as a lambda expression. Function definitions are enclosed in curly brackets {}
. Argument declarations, if any, follow the open bracket. Variable declarations follow argument declarations. An expression follows the declarations.
Functions are not evaluated immediately when they occur in code, but are themselves objects that can be passed around.
A function object may be evaluated by passing it the value
message, or its shorthand .()
, and a list of arguments.
When evaluated, the function object returns the final value of its expression.
An empty function object returns the value nil when evaluated.
Arguments can also be provided by name.
A function can be thought as a machine able to perform a task on demand, e.g. a calculator. The calculator can receive input (args) and can output a value, the result of the performed operations. The function definition can then be thought as the building of the calculator: once built, the calculator does nothing until it is requested to work (by passing the value method to a function). The following figure depicts an empty function, input without output, output without input, and the general case with input and output.
An argument list immediately follows the open curly bracket of a function definition. An argument list either begins with the reserved word arg
, or is contained between two vertical bars. If a function takes no arguments, then the argument list may be omitted.
Names of arguments in the list may be initialized to a default value using the following syntax forms. Arguments which are not explicitly initialized will be set to nil if no value is passed for them.
"arg" style, default value is a literal: { arg x = 1; .... }
[1]
"arg" style, default value is an expression: { arg x = 10.rand; ... }
[2]
"arg" style, default value is a literal but you want to treat it like an expression: { arg x = (2); ... }
[2]
Pipe style, default value is a literal: { |x = 1| ... }
[1]
Pipe style, default value is an expression: { |x = (10.rand)| ... }
[2]
In general arguments may be initialized to literals or expressions, but in the case of Function: -play or SynthDef: -play, they may only be initialized to literals.
See Literals for more information.
Variable arguments are how functions catch an unknown number of arguments or keyword arguments. There are two possible variable arguments, the first for positional arguments, and second for keyword arguments. They are marked by an ellipsis after any normal arguments and separated by a comma.
The first variable argument (positional) catches all remaining arguments in an Array
The second variable argument catches all keyword arguments. It puts them in an Array of key-value pairs.
The use of variable keyword arguments is often paired with Object: -performArgs.
While you can pick any name for the variable arguments, they always refer to the unmatched positional and keyword arguments, so it is often best to name them args
and kwargs
.
You cannot reassign the value of the variable arguments at the call–site even if they share the same name (see below for example).
Argument defaults that are literals are stored as part of the FunctionDef. Arguments passed at runtime — including nil — always override the defaults:
Since expressions are evaluated when the function is called, they cannot be stored in the FunctionDef. They are executed only if the passed-in value is nil.
This means you can use expression-style to define a default that cannot be overridden by nil.
Note: Parentheses are required when initializing an argument to an expression, if the argument list is written inside ||
pipes.
This is because the pipe character also serves as a binary operator. Without parentheses, expressions such as the following are ambiguous:
The following produce identical function definitions. Expression-style defaults are simply a shortcut syntax for the latter.
Following the argument declarations are the variable declarations. These may be declared in any order. Variable lists are preceded by the reserved word var
. There can be multiple var declaration lists if necessary. Variables may be initialized to default values in the same way as arguments. Variable declarations lists may not contain an ellipsis.