Functions - A technical question

Let’s say you have some one-off functions that you run for very specific needs, such as running through an array of something (maybe Parts or UI elements), and then running a function for each one individually (whatever your reasons might be). After you don’t need the function anymore, could you set the function variable to nil to remove unnecessary data, or does this not do anything at all?

I don’t recall functions retaining information like that unless specifically made to do so. Can you give an example?

Yes and no. You are able to revert your claim on said functions name in the global namespace by acting as if it were a variable, like so:

function someFunction
    print("I do something!")
end
someFunction = nil

However this is not something you want to do. There is no good reason to revoke the namespace of your function once it’s created. It hardly takes any processing or storage ability to keep it there, and what’s more is that roblox’s engine does in fact remove data you’re no longer working with automatically, it is rarely ever used on functions because most of the time there is no reason to.

Again, please never do this.

Sincerely, anyone that has to look at your code with their own human eyeballs and doesn’t want nightmares.

1 Like

It’s not strictly necessary. If the function goes out of “scope” and isn’t otherwise reachable from somewhere, e.g. it’s not a global variable (function foo() end, not local function foo() end), then the garbage collector will do a better job of getting rid of it than you can.

As RepValor says, it also hurts readability, because it’s extremely uncommon, because it’s not necessary.

If you’re really concerned about leaving garbage around, then you could use a do block to make a scope specifically for it:

local array = {1, 2, 3, 4}

function apply(array, func)
	for i = 1, #array do
		array[i] = func(array[i])
	end
end

do -- locals within a block are not reachable outside it
	local function foo(n)
		return n*n*n
	end
	
	apply(array, foo)
end

-- foo may as well not exist anymore
print(foo) -- nil
2 Likes

All variables use memory even in functions. What I tend to do is define variables that I can change for all my functions. The Parameters however are not saved as they are passing through the function.

local FVar1
local FVar2
-- And whatever else I want

function Func1(DoStuff)
    FVar1 = -- Whatever You want to store
    -- Do Stuff
    FVar1 = nil
end

This will use the variables and then set them to nothing.
Another trick you can do is make a table so one variable can hold what would technically multiple

rather than doing
local Var1 = – Value1
local Var2 = – Value2

you can do
local VarData = {–Value1,–Value2}
and this means you can get around the limit on number of variables as well(200 local vars)

it makes managing your variables slightly easier too.
To get the variable you can just do VarData[number]

(You could also make it a dictionary and enter the names of theoretical variables there.)