r/computerscience • u/ShadowGuyinRealLife • 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
View all comments
Show parent comment
1
u/tmzem May 24 '25
With TCO, it only "works". If you want it to actually work in practice, you also need some kind of "tailcall" keyword which errors out if the call is not actually in tail position. Otherwise, even innocent refactors can take away the tail property and lead to stack overflows later on, so without the keyword it's hard to test the code for correctness.