Search code examples
luagarrys-mod

Difference in functions


I am just curious if there is a difference between these two different types of functions.

function PrintHello()
    return print("Hello")
end

and

PrintHello = function()
    return print("Hello")

Solution

  • Beside that you're missing an end in the second function both snippets are equivalent.

    According to the Lua 5.4 Reference Manual 3.4.11 - Function Definitions

    function PrintHello ()
      print("Hello")
    end
    

    translates to

    PrintHello = function ()
      print("Hello")
    end
    

    As you'll also find in the manual there is a difference for local functions.

    local function a() end
    

    translates to

    local function a;
    a = function () end
    

    This allows a to reference itself for example in a recursive call.

    Instead of return print("Hello") simply write print("Hello"). print has no return value.