Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is it more efficient to declare a function within the global scope or a local scope of a script?

Quick question. I wanted to go about calling a function while yielding another in order to shorten up code, like so:

--NOTE: This is just an example, and has no real practical uses
--f represents function #1
function example(conditional)
	local var = 0
	if conditional then
		var = f(var)
	else
		var = f(var+1)
	end
	return var
end

Though I didn't know if declaring function f listed above in the scope of the function example would be more efficient than in the global scope.

2 comments

0

I don't understand the question. You never declared "function f".

0

Read the script comment. "f represents function #1". Just imagine f is declared somewhere. What I'm asking is what scope I should define it in.

1 answer

✓ Accepted answer

Well if you're only using it in that function, then don't define a second function at all. Write the second function's code directly into that function instead, making it only 1 function. That’s more efficient. But if you don't want to do that, then define it locally right above it, not globally.

However, even if efficiency might differ in multiples, it's pretty much negligible if you're considering game resources.

This discussion is preserved as it was. New answers, comments, and votes are closed.