Anonymous functions in Lua

ยท

1 min read

If you have been wondering how to write anonymous functions in Lua, here are examples below:

As an inline argument:

do(function()
    print("Hello!")
end)

As variable:

myfunc = function(n)
    return n*n
end

-- example of call
myfunc(5)

-- example of use as argument
do(myfunc)

If you are looking to learn Lua or Love2d the game engine, check out my introductory guide below:

ย