Function Definition
Named Functions
Define a named function with the following syntax:
def functionName(args) type {
...
}
where:
functionNameis the name of the function.- If
functionNameis already defined as a function or other value, it will only be overwritten if the type does not change.
- If
argsis multiple comma-seperated pairs of:- a type identifier for the parameter, and
- an identifier (representing the parameter name)
typeis the type of value which the function will return...is the code inside the function (the function body).- The function body will have access to previously set variables through a closure. The closure is a copy of all variables in their current state at function definition time.
- The function body will also have access to the parameters defined in
args.
Example:
def add(int a, int b) int {
return a + b
}
Anonymous/Lambda functions
Define a lambda function with the following syntax:
lambda(args) type {
...
}
where:
argsis multiple comma-seperated pairs of:- a type identifier for the parameter, and
- an identifier (representing the parameter name)
typeis the type of value which the function will return...is the code inside the function (the function body).- The function body will have access to previously set variables through a closure. The closure is a copy of all variables in their current state at function definition time.
- The function body will also have access to the parameters defined in
args.
Example:
add = lambda(int a, int b) int {
return a + b
}
Lambda functions are most useful when passed as arguments to other functions.