When do i use local and non-local functions?

Why wouldn’t it prevent name collision, then? I thought name collision was unintentional overwriting.

I think you misunderstood.

local function f()

and

local f
f = function()

are the same, but

local f = function()

is not. In the case of the latter, f will be an undeclared variable from inside the function, resulting in an error if you run this.

local f = function()
    f()
end
f()
1 Like

Oh, I haven’t noticed that these were different. You are right; I said they are the same because OP was talking about local and global variables difference

1 Like

You can for example create decorators:

function decorator(func)
    local i = 0
    local function internal(...)
        i = i + 1
        print("function was called " .. i .. " times")
        return func(...)
    end
    return internal
end

function anyFunction(n)
    return n * n + n
end

function otherFunction(x)
    return x .. x
end

anyFunction = decorator(anyFunction)
otherFunction = decorator(otherFunction)

print(anyFunction(5))
print(anyFunction(2))
print(otherFunction("test"))

output:

function was called 1 times
30
function was called 2 times
6
function was called 1 times
testtest

It depends on intentionality. Intentionally shadowing a variable prevents name-collision bugs/errors, but accidentally doing so causes them.

It’s literally the traditional name collision problem in reverse: you’re NOT overwriting a higher-scoped variable when you WANT to, leading to bugs.

1 Like

True, but I would call that probelm a separate one from name collision. Using local does prevent name collision, but doesn’t prevent this problem.

difference is speed local functions or variables tend to be more fast than gloabls ones

The difference is negligible, not fast.

That is, what I said in my first post.
We were talking about forward declaration, in the post that you replied to, not local vs global variables.

So are there any solutions to the question I asked?

Just use local variables all the time, you will only use global variables/function when you need to share a variable/function in the entire script. Using local variables/functions prevents name collision, and is, though insignificantly, faster than using global variables.

So every function in every script should be local

Unless you have an explicit reason to not do so, that is correct. And if you don’t know what those reasons might be, you do not have one.

I heard something about it being best to use the this method
local function func()

end)

something about in not being exploitable. I’m not exactly sure and could be making this all up, just something I heard and stuck to. But there isn’t a difference when it comes to normal use.

Yes but I like making variables for functions like local Function = function()

And that’s alright, not saying you should switch. Just saying why I use what I use.

What? There is literally nothing exploitable using global functions and yes, there is a difference when using global and local functions.

I’m asking when do I use them like when do I use a local function over a non-local one?

Always use local functions over globals, only should you use globals when you want to call the function anywhere in the script .

A local function defined as such…

local function ABC() end

…is literally just syntactic sugar for:

local ABC
ABC = function() end

I would prefer using local function ABC() ... end in most cases unless you’re forced to assign a function to the variable at a different time.


In terms of when to use them: always. Always prefer local variables over global. They’re faster for Lua to access due to the different memory locations used for each (albeit it’s a small difference, but good practice nonetheless).

6 Likes