Difference in functions

68 Views Asked by At

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")
2

There are 2 best solutions below

1
On BEST ANSWER

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.

1
On

function PrintHello () print("Hello") end