What is the difference between
local function thisisalocalfunction() end
and
function thisisafunction() end
???
I am just wondering, is there a difference or is it basically the same?
Thank you!
What is the difference between
local function thisisalocalfunction() end
and
function thisisafunction() end
???
I am just wondering, is there a difference or is it basically the same?
Thank you!
There isn’t much of a difference…
Except ,you can nest a local function
inside a normal function and make it “actually local”:
(Just as an example!)
function Test()
function Test2()
print("123") -->>> 123
end
end
Test()
Test2()
function Test()
local function Test2()
print("123")
end
end
Test2() --- won't work
i would recommend looking into the link above it answers your question a little better
the only difference is that one is global and one is not. This means that a global function can be referenced throughout the entire script while local functions only can be referenced in the same block of script.
for example
function:
Test() -- Prints "a"
function Test()
print("a")
end
local function
Test() -- errors
local function Test()
print("a")
end
In your first example you call the function before execution reaches the point where it’s defined; this would cause an error. A better example could be done using scopes.
do
function Test()
print('yes')
end
end
Test() -- prints 'yes'
do
local function Test()
print('no')
end
end
Test() -- errors, 'Test' is only in the 'do end' scope
Remember that Lua has no concept of a “local function” or a “global function”.
All Lua functions are anonymous, which means they have no names.
This code:
function test()
end
Expands to:
test = function()
end
And this:
local function test()
end
Expands to this:
local test
test = function()
end
if you’re wondering why it doesn’t expand to this:
local test = function()
end
That is because then you wouldn’t be able to make recursive calls.
Functions are first-class like any other conventional datatype. They are not special at all.