To be honest, Lua doesn't have a function like a named function. All functions are actually anonymous, but can be stored in variables (which have a name).
The named function syntax function add(a,b) return a+b end is actually the syntactic sugar for add = function(a,b) return a+b end .
Functions are often used as event handlers and for solutions that the library does not know / cannot know, the most famous example is table.sort() - with your function you can specify the sort order:
people = {{name="John", age=20}, {name="Ann", age=25}} table.sort(people, function (a,b) return a.name < b.name end)
The fact is that, most likely, you will not need a function later. Of course, you can also save the function in a (possibly local) variable and use it:
local nameComparator = function (a,b) return a.name < b.name end table.sort(people, nameComparator)
For more information, read the section on functions in PiL .
source share