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

25

u/Yoghurt42 May 22 '25

Another good example is solving Towers of Hanoi. Trying to solve it without any kind of recursion/stack will make the code really difficult to follow, but written recursively it's completely trivial.

3

u/scalesuite May 22 '25

Beat me to it! Just commented this same thought.