Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

"Local Function" and "Function" are the same thing correct? Local Function is just a variable?

Asked by 6 years ago

I have searched up YouTube videos and I can't seem to find what I am looking for.

3 answers

Log in to vote
3
Answered by
Avigant 2374 Moderation Voter Community Moderator
6 years ago

It's important to remember that functions are first-class values, and that all functions in Lua are anonymous (nameless). This code:

function name()

end

Is really sugar syntax (a nicer way of writing something else) for this:

name = function()

end

And that this code:

local function name()

end

Is really sugar syntax for this:

local name
name = function()

end

You might ask why it's not sugar syntax for this instead:

local name = function()

end

And the reason would be that you wouldn't be able to make recursive calls in the function if that were the case.

Again, functions are just like any other values in Lua. You can store them in variables, pass them to other functions, and so on, they're not special at all.

Ad
Log in to vote
4
Answered by
mattscy 3725 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

Like the difference between local variables and global variables, local functions can only be seen within their scope. For example, if you try to call a local function from above it, it wont work, while it will with a normal function.

E.g. this function will not run:

Banana()
local function Banana()
    print("run")
end

While this one will:

Banana()
function Banana()
    print("run")
end

You should make functions local as often as possible, as it can be more efficient.

Log in to vote
-1
Answered by 6 years ago

It's like the difference between local __ = x and __ = x Pretty much the same, yes.

Answer this question