Functions
A function is essentially a named chunk of code. Unlike other chunks, the contents of a function are not automatically executed when the file is loaded. When a file is first loaded, functions are simply defined. Once a function has been defined, you can execute the function by calling it. Because a function is a named chunk, you can call a function as many times as you want. The same scope rules apply to functions as to do/end blocks.
Note
Read more about functions online at https://www.lua.org/pil/5.html.
Defining a function
A function declaration starts with the function
keyword. After the function keyword, you provide thefunction name. The name of the function follows the same naming rules as the name of a variable.
After the name of your function, you have to provide a list of parameters. Parameters are variable names enclosed in parentheses ()
. The list of parameters may be empty if a function needs no parameters, in which case only opening and closing parentheses are given—...