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

Can anyone help me understand functions, I'm struggling?

Asked by
s_21 74
6 years ago

I'm not a scripter, But my mates been teaching me and Asked me a question which I'm struggling to answer

Given function c (enclosed in function f with environment e), a reference to an instance of c (as z) (created from a call tof) has its environment set to k: local function f() local function c() end return c end setfenv(f, e) local z = f() setfenv(z, k) # Which of the following is always true:

a. z can still access all the locals of f that it initially had access to b. z can still access all the globals of f that it initially had access to c. All further new closures of c will have environment k d. None of the above

Please can someone help me and try explain why the answer is that so I can understand more?

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

basically, a function is something that encloses code inside of it, some "parameters" can be passed, which are essentially variable defined to suit whatever occasion nessasery

take this function:

function Print2Times(text)
    print(text..text)
end

when we say "function", we are telling the lua interpereter that we are creating a function, "Print2Times" being the name of the function, and "text" being a variable, inside of the function is some code that reads:

print(text..text)

notice how we reference the "text" variable from earlier? text..text returns whatever the value of the text is joined with itself, so if text = "Hello", then text..text = "HelloHello", now lets call the function, replacing "text" with any string we want (enclosed within quotation marks of course)

Print2Times("Hello World!")

the function we defined will run, with the variable "text" being equal to the string "Hello World!",

whats cool is that you can use a "return" statement to set a variable to whatever the function returns

function GiveMeANumber()
    return math.random(1,10)
end
local a = GiveMeANumber()

print(a)

>7

as for the question, any variables defined directly within the function cannot be accessed outside because it is outside of the scope, it can only access variables passed and variables outside of it

local a = "j"
function lol()
    print(a)
end

>a

function lol2()
    local f = "foo" .. 44
    print(f)
end

f = a

>Error

but if its in different enviroments then you cant access each others variables unless it is part of _G, the global variable table, or if you comminucate via Bindable/Remote Events/Functions

Ad
Log in to vote
0
Answered by
Amiaa16 3227 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

Edit: if you have no basic idea of how a function works, refer to the other guy's answer! In this answer I explain the solution to the question you posted.

Here is your question formatted in a readable way:

--Given function c (enclosed in function f with environment e), a reference to an instance of c (as z) (created from a call to f) has its environment set to k:
local function f()
    local function c()
    end
    return c
end
setfenv(f, e)
local z = f()
setfenv(z, k)
--[[# Which of the following is always true:

a. z can still access all the locals of f that it initially had access to
b. z can still access all the globals of f that it initially had access to
c. All further new closures of c will have environment k
d. None of the above]]

I believe the answer is: a. z can still access all the locals of f that it initially had access to

No matter how you alter the env of a function, it will always keep access to the locals and upvalues it originally had access to.

Answer B cannot be true, because the env of z is changed, therefor it has different global variables, as global variables are determined by the env.

Answer C cannot be true, because f creates a new instance of function c each time it's called. Meaing f() ~= f(). It simply creates a new function each time, and the script changes the env of only one of those created functions.

Answer D cannot be true because answer A is correct :p

Answer this question