User-defined functions
A user-defined function is a reusable piece of logic declared within a script. Once declared, the function can be called like any built-in function.
Declaration
A function declaration begins with the FUNC keyword, followed by a name, a parameter list in parentheses, and a body.
There are two body forms: arrow and block.
Arrow form
The arrow form uses => followed by a single expression. The result of the expression is the return value.
Use the arrow form when the function body is a single expression.
Block form
Unlike many C-like languages, FQL uses parentheses for block function bodies.
Use the block form when the function body requires intermediate variables or multiple steps.
Parameters
Parameters are listed inside parentheses, separated by commas.
A function may have no parameters:
Parameters are positional. The caller must provide exactly the number of arguments the function expects.
Capturing outer variables
A function body can read variables from the enclosing scope.
If the outer variable is declared with VAR, the function can also modify it:
Variables declared with LET are immutable and cannot be reassigned inside a function.
Nesting functions
Functions can be declared inside other functions.
A nested function can access variables from all enclosing scopes, not just the immediately surrounding one.
Using functions in loops
User-defined functions work naturally with FOR loops and other query constructs.
Function names
Function names follow the same rules as variable names: they must start with a letter or underscore, followed by any combination of letters, digits, and underscores.
Function names are case-sensitive. add and Add are different functions.
By convention, built-in functions are written in uppercase, while user-defined function names may use the style preferred by the script author.