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

Can a function call itself?

I want to know if a function can call itself.. can it?

If so, how?

3 answers

✓ Accepted answer

Recursion


Yes, it's possible. What you're referring to is called recursion. If a function is calling itself, it's a recursive function. Most of the time, people opt for the use of recursion because it may be a shorter, more human-readable solution. However, that doesn't make it easier for the program.

Should I use recursion?

It depends. More often than not, you shouldn't need to have a function call itself. You can either have a separate function handle the task, or if you're trying to traverse a data structure (table, object, etc), you can use a loop.

Is recursion bad?

In short, and for most circumstances, yes. Recursion can be very costly. This of course depends on how deep the recursion is, but most of the time it exceeds practicality. Whenever the function calls itself again, it has to create a new address in memory to allocate all the local variables and instructions within that function. All of this data is stored in the stack, which is a special place in memory that stores information like local variables and return addresses. Eventually, however, the stack can get overwhelmed with addresses, and result in the infamous stack overflow error.

Conclusion

Try to avoid recursion. Loops are quicker, and more memory efficient. This was probably a lot of new information to take in at once, so if you have any questions, just let me know.

Disclaimer

Like in every situation, it depends on how it's implemented. Just because a function is calling itself, doesn't automatically mean it's inefficient. You want to avoid recurring function calls when they start to result in a tight loop. For example, this would be a poor implementation of recursion:

local function foo()
	foo() -- foo call itself, which calls foo, which calls itself...
end

foo() -- Call foo

Whereas this example is only calling itself once to toggle gate until it's false (this implementation is known as debouce).

local gate = false

local function foo()
	gate = not gate
	if gate then
		foo()
	end
end

foo()

It can, but be carefull. if you dont add a return, or a wait (or any other form of yielding) it will lag out and crash.

The example below will find every instance inside o, and throw them into a table. Constantly using the same function again to find everything inside everything.

function GetDescendants(o)
	
	local AllObjects = {}
	
	function FindChildren(Object)
	   for _,v in pairs(Object:GetChildren()) do
			table.insert(AllObjects,v)
	        FindChildren(v)
	    end
	end
	
	if o then 
		if typeof(o) == "Instance" then
			if o.Parent ~= nil then
				FindChildren(o)
			end
		end
	end
	
	return AllObjects
end

5 comments

0

Would this example run over and over - forever? Because when it is called, it calls itself over and over, right?

0

it will not

0

it will not call itself infinitely, you will cause a stack overflow. When a function calls itself it is called recursion http://wiki.roblox.com/index.php?title=Recursion

0

There's no point doing it like this anyways, and it will overflow regardless of whether there's a wait or not.

0

i talked to him in chat about what he needed, we discussed if, break, returns to cancel out the function.

Using something almost anyone hates: Recursion, however it can be quite useful sometimes. When I made an explorer I used recursive functions.

1 comment

0

Speak for yourself.

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