Funcname = function()

i saw somebody use this:

local functionname = function()
    -- stuff
end

instead of this:

local function functionname()
   -- stuff
end

is this an actual thing, and if so, what is the difference between the two?

not sure if i put this in the right category or not

It is the same exact thing… its also used in other programming languages.
i.e. Javascript

const example_function = function() {
    console.warn("This is an example!")
}

is the same as:

function example_function() {
   console.warn("This is an example!")
}

If I am not mistaken I think what it does is just sets a function to a variable. It possibly by theory would work but I would personally just stick to the normal way doing it.

I am pretty sure it is the exact same thing just written differently. I found this post answer which explains it but it is legit just the same if I am not mistaken: What is the difference between a local function and a normal function? - #3 by XAXA

So An anonymous function expression is a function without a name that is assigned to a variable. You access and call the function through the variable to which it is assigned.

On the other hand, a named function declaration is a function with a name that is directly defined in the current scope. You can call the function using its name.

Both ways of defining functions are valid, but the main difference is in how you access and call them. With an anonymous function expression, you access and call the function through the variable to which it is assigned. With a named function declaration, you directly call the function using its name…

1 Like

defining a function like this gives the function access to the func variable, it’s also the prefered way to define a function as it is more readable.

local function func()
    print("hey")
    return func
end

func()()

local func2 = function()
    print("hey")
    return func2
end

-- will error because the function did not have access to the func2 variable
func2()() 

you can still achieve this with the other method by doing it like this, but there’s no reason to do so.

local func2
func2 = function()
    print("hey")
    return func2
end

func2()()