r/C_Programming • u/yug_jain29 • Jul 10 '26
Question can I skip recursive functions?
a code i can run with the logic of iterative, so why do I have to learn the new concept as complicated as recursive? (ik it's one of important questions in c)
if yes will u pls explain it with a very realistic and simple example
thanks a lot 🙏
0
Upvotes
View all comments
2
u/Ch1noXL Jul 10 '26 edited Jul 10 '26
Think of a program where you need to list all of the files found in a directory, including all of its subdirectories and the sub-subdirectories, so on and so forth.
This can be done using recursion.
You write a recursive function that:
What this does is it will search through the directory and each of its subdirectories for files until it gets to a point where there are no more subdirectories to search through.
You could do this with loops, but it would be overly complicated for a scenario like this.
This was the scenario I was put in during an internship and what helped recursion click for me.
Edit: Fixed wording in step 3, had the yes and no cases backwards before.