So I’ve got a question about functions it’s hard to explain

Sorry my phone was low battery so code won’t formatted that well anyways so it’s a question about the scopes of functions

Blockquote

Local function A()
Warn(Foo1) — this will prints 5 without an error
end

Foo1 = 5 — or local—
A()

And then if I do this

Local b = function() — different syntax’s
Warn(foo2) — this just outputs nil unless I do
Warn(getfenv().foo2) — this works
end

Local foo2 = 10 —local or not error is thrown
b()

Blockquote

1 Like

This is quite hard to understand since you haven’t used a code snippet, make with three backticks `. Could you send the full code under the backticks?

How do I format the code? I don’t get it

You could either put three backticks either side of it, which is the ` symbol, or click the ‘</>’ button at the top of the post composing section.

From what I can tell, it looks like you are not making the function return a value, leaving it as nil.

I know that you can declare the variable before
Hand and paramaters my question is I’ve noticed that theirs different behaviour between

Local var = your function

And

Local function name()

I’ve been coding on lua for like 2+ years and I am embarrassed that I didn’t know these differ somehow I feel I should know this already

Formatted code
local function A()
	warn(foo1)
end

foo1 = 5
A() --> 5

local B = function()
	warn(foo2)
	warn(getfenv().foo2)
end

local foo2 = 10
B()
--[[
	if foo2 is local:
		--> nil nil
	if foo2 is global:
		--> 10 10
]]

As the lua documentation explains, “the scope of a local variable […] lasts until the last non-void statement of the innermost block that includes the declaration”. (Source: Lua 5.3 Reference Manual)

Therefore, in simpler terms, a variable declared in a script evironment (outside functions, do-blocks, and similar limited scopes) will be accessible anywhere after the declaration.

print(a) --> nil (unless a is a global variable)
local a = true
print(a) --> true

A variable declared without the keyword local is a global variable accessible anywhere in the script context, but comes at a cost of worse performance. Local variables are always faster to work with.

A function, no matter how it’s defined, is also referenced by a variable - its name.

local function fn() end
local fn = function() end

Provided that a function is referenced in a global variable, e.g. function fn() end, it can even be accessed by functions defined earlier in the script (which wouldn’t be possible if it was local).

local function fn1(arg) fn2("something") end
function fn2(arg) print(arg) end --> a local function would error
fn1() --> something

Now, getfenv() has the capacity to access other environments, for instance the environment wherever the function that used getfenv() was called. It was recently deprecated and causes deoptimisations because the compiler has to treat functions that use it as impure.

Edit. @swag4lifex sorry, didn’t see your last reply before posting. Tl;dr: there shouldn’t be any difference. Perhaps the error was thrown because of a typo.

2 Likes

No problem I constantly study code and I can always improve my knowledge i’de rather have in depth understand of a topic and your post helped me get 1 step closer to “mastering” lua if that’s possible

Functions are one of those thing where I understand them but not fully or to the extent I could if I looked into them enough

i didn’t know that getfenv was deprecated thought is that new? i mean i knew that it was something you should avoid but i never knew it was straight out Deprecated

and their was some helpful information that you stated that i was somewhat blind to

1 Like

I’m glad I could help! :slight_smile:

Yes, it was deprecated in November. Doesn’t mean it will be removed, but it’s a formal sign that it’s discouraged where you don’t have to use it.

I drew the information about how it prevents optimisations from Performance - Luau. It’s a contradiction to pure functions because, at the very least, it reaches for changing/mutable variables outside of the function.

Just a bit more about functions

When you create a function, it gets its place somewhere in memory, and the reference is stored in a variable. Same goes for tables and threads.

local function Function() end
local SameFunction = Function
print(Function) --> function: 0xce2aa197543abd66
print(SameFunction) --> function: 0xce2aa197543abd66

print(function() end) --> function: 0xcdf879cace56eac6
print(function() end) --> function: 0x4acd3c3df8cb5326

Function is just like other variables and stores something. In this case it stores a function reference. So one could imagine functions like buildings where something is done with data and variables have the address.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.