Functions
Functions are reusable blocks of code that may accept parameters and optionally return a value. Every executable Azin program begins execution in the main function.
Declaring Functions
Functions are declared using the fn keyword.
A function declaration consists of:
- the
fnkeyword, - the function name,
- an optional parameter list,
- an optional return type,
- the function body.
The function body begins with do and ends with end.
Zero-Parameter Functions
Functions that do not accept parameters omit the parameter list.
Functions without parameters and without a return value are written as follows.
The following declarations are equivalent.
The parameter-less form is available only for functions that accept no parameters.
Parameters
Parameters are declared inside parentheses.
Multiple parameters are separated by commas.
Each parameter consists of an identifier followed by its type.
Parameter names must be unique within the same function declaration.
The declaration above is invalid because both parameters use the same identifier.
Parameters may shadow identifiers from outer scopes.
Return Types
A return type is specified after the parameter list using :.
If no return type is specified, the function does not return a value.
Returning
Use the return statement to exit a function.
Functions with a return type must return a value compatible with the declared type.
Functions without a return type may use return; to exit early.
Returning a value from a function that does not declare a return type is invalid.
Calling Functions
Functions that accept one or more parameters are always invoked using parentheses.
Zero-parameter functions may be invoked either with or without parentheses.
Both forms are equivalent.
Function References
Functions are first-class values.
Whenever a value of type fn is expected, the function name is interpreted as a function reference.
Functions may be passed as arguments.
Functions may also be stored in variables.
No additional syntax is required to obtain a function reference.
In every other context, referencing a zero-parameter function automatically invokes it.
The examples above are equivalent to:
Generic Functions
Generic functions declare one or more type parameters using square brackets.
Function Overloading
Multiple functions may share the same name provided that their parameter lists differ.
fn add(a: int, b: int): int do
return a + b;
end
fn add(a: float, b: float): float do
return a + b;
end
The compiler selects the appropriate overload based on the provided arguments.
Examples
A simple function.
A function with parameters.
A function that returns a value.
Passing a function as an argument.
Storing a function reference.
Design Goals
The function system is designed around a small set of principles.
- Simple, readable declarations.
- Minimal punctuation.
- Concise syntax for zero-parameter functions.
- Functions are first-class values.
- No dedicated syntax is required for function references.
- Function calls remain concise without sacrificing readability.
- Explicit declarations are preferred over implicit language magic.