A function can be declared as local
to limit the scope of the function. A function is global by default.
It's a good practice to use local
whenever possible to limit the scope of functions and variables to where they are needed. It allows for a flexible and modular approach in designing functions.
Expand | |||||
---|---|---|---|---|---|
| |||||
If you were to create a simple APP.lua file and add a function called APPadd like below:
In main you can then call APPadd and get the sum: |
Expand | |||||
---|---|---|---|---|---|
| |||||
In the same APP.lua file, now alter the function to be
Note: the The function is now local, but we have also returned the APPadd function to create a closure - the values of Now in In main, you can capture the closure returned function in your require statement , and use it as if it were APPadd. : |