r/computerscience • • May 22 '25

Discussion Why Are Recursive Functions Used?

Why are recursive functions sometimes used? If you want to do something multiple times, wouldn't a "while" loop in C and it's equivalent in other languages be enough? I am not talking about nested data structures like linked lists where each node has data and a pointed to another node, but a function which calls itself.

106 Upvotes

152 comments sorted by

View all comments

Show parent comment

2

u/WittyStick May 22 '25

Iterative solutions are preferred because most code is written in languages which don't do tail call elimination, so recursive solutions blow up the stack.

When you have proper tail calls, recursion feels natural and preferable to iterative solutions.