Question on Local Functions

So i have been reading the “programming in lua first edition” and have been learning things on a deeper level.

When I do this:

local function unpack()
    --do stuff
end

unpack()

and then this

function unpack()
   -- do stuff
end

It doesn’t work and I have to make it local. The only conclusion that I came to was i wasn’t making it local to that script, therefore overwriting the global variable unpack(). However if i do x = 10 in one script, and in another i do print(x), it prints nil meaning that the global variable is only local to that script so it doesn’t make a difference if i put local or make it global. So why can’t I do this with the function scenario?

Functions are just variables and they behave the same as any other. Your code should work and I’m not sure why it wouldn’t. For what it’s worth though, unpack is a built-in function and you should avoid naming anything unpack to prevent confusion.

Local variables are specific to the scope they are declared in.

function test()
    a = 10
end
test()
print(a) --> 10

function test2()
    local b = 20
end
test2()
print(b) --> nil because b is local and so it can't be accessed from outside of test2.
1 Like

Oh i realised it had a red line underneath it and when i actually tested the code, it worked.
Edit: basically this post was a waste of time.