Using Functions as loops? Bad practice or just preference?

I came across something interesting today and realized that the system in which loops are used for can actually be “replicated” using a certain function structure.

My initial question, as the rest of this post’s content is simply a demonstration, is:

Is there a potential drawback of using “function loops” ASIDE normal-type loops (While, for, repeat)
(I use normal loops for pretty much every table-related operation in my game currently (Such as loading datastore keys or for iterating over an instance’s children/descendants)

Does using this ‘function loop’ present a bad practice? (I personally don’t have any issues at all with it) Worse performance or is it simply not convenient?

The point is, I have several scripts using these function loops (mainly for instances as of now, but I don’t think I feel confident without truly knowing what could be of loops and loop function differences.

The initial loop function only has to be called once and then cycle begins, just as how once a loop in started, it only has to be called once to continue until both of their target variables have reached a certain value.

Should I abandon the idea of integrating “looping functions?” I assume as of now that unless iterating for table/object searches or when the index value is quite high (Like counting down from 1,000+ Seconds and would be better placed in a loop, but then again -

I don’t think calling a minor function to count down every second from 1,000 would be any more demanding than a loop counting down for the same amount of time.


Is practically the same as:

Yes. There’s one huge drawback to doing this. (By the way, the term for what you’re doing is recursion).

Recursion’s biggest drawback is that it can quickly lead to a call-stack overflow. It is from this exact problem that the popular software engineering forum StackOverflow gets its name.

You’re unlikely to run into this problem in examples like the one listed here, but it is certainly a possibility with less finite bounds on the recursion.

As for personal preference… Loops are almost always preferable to recursion.


This is true. A couple years ago, we had tail-call support, which would let you do recursion without overflowing the stack. But that was removed (due to something related to debugging). Using a loop is preferable.


Yeahh if we had tail-call support then some cases of recursion would avoid this stack overflow problem altogether. But it doesn’t sound like Roblox will be adding support to tail-calls any time soon so yeah, stick with iteration (loops) over recursion.

1 Like

Alright. Thank you so much for informing me! I actually did run into a stack overflow error a few days ago on my actual place and thankfully I didn’t (and don’t have any more recursive functions) know exactly what was causing it.

I’d like to add that recursive loops can sometimes be hard to find. In the past, I’ve personally run into stack overflow errors for long-running servers when the game loop is implemented with a loop of chained function calls (e.g. startRound -> startMinigame -> more bad code -> endRound -> lobby -> startRound).

As a result, I’d highly suggest that wherein you might find yourself creating deep call-stacks in a less direct manner like the above example, it is better to either convert to iteration (perhaps a while true do loop) or (my preference) an event based system.

1 Like