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
11
u/DTux5249 May 22 '25
Recursivity let you solve many problems very elegantly. They're often incredibly readable, because they break stuff down clearly into:
You also dismiss data structures, but that's kinda short sighted. Most functions operate on or in tandem with data structures. We use a ton that are inherently recursive; trees and graphs are incredibly common. Their structures often make recursion easier to conceptualize.
That said, iteration is preferred. It's much more flexible, as well as safer.