Functions
Like Lua, functions are first-class values in Zaidlang. That means that functions can be stored in variables, passed as arguments to other functions, and returned as results. This gives great flexibility to the language.
Defining Functions
You define functions using the function
statement, followed by a list of parameters, and a body:
The body of a function is always a block. Inside it, you can return a value using a return
statement.
Calling Functions
Once you have a function, calling it is as simple as passing the required parameters along with the function name:
The assigned value is the result of either an explicit return
statement or the last value of the function's body.
Anonoymous Functions
Functions are first class in Zaidlang, which just means they are real values that you can get a reference to, store in variables, pass around, etc.
Since function declarations are statements, you can declare local functions inside another function:
You can even combine local functions, first-class functions, and block scope:
Last updated