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

Is efficient to call a function inside another function?

Let's say I have multiple functions like so:

local function1 = function()
	local printMessage = "Hi! This is function 1"

	return printMessage
end

local function2 = function()
	local printMessage = function1()

	print (printMessage)
end

Would this be just as efficient as doing:

local function1 = function()
	local printMessage = "Hi! This is just a function!"

	print (printMessage)
end

I'm trying to keep my code as clean, but as efficient as possible. Thanks in advance for any advice.

edit:

I know you're probably thinking, "why not just do one function?"

This is just an oversimplified version of what I'm doing using print as an example.

1 answer

✓ Accepted answer

In the simplest way, it can be efficient.
For example if you have a 15+ line function, and it needs to be called multiple times, in different places, then yes, it would be effective to use a function in a function.
However if you only ever use the function once in your code then it should be simplified all into the one place/function where the code is required.
Remember to keep it short and effective as possible.

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