Which is the best fit statement for recursion v iteration?
Recursion is always better than iteration.
Recursion uses more memory compared to iteration.
There is no difference between recursion and iteration.
Iteration is always better and simpler than recursion
I feel like the only real truth is that recursion uses more memory compared to iteration. Sometimes it seems preferable to use recursion over iteration. But I'm not too sure.
CodePudding user response:
Main difference between recursion and iteration how they operate, check this link Difference between Recursion and Iteration
CodePudding user response:
Yes in this case you're correct. Every time you call a recursive function, it stores memory in the stack structure. A stack is like a box that one end opened; we put an item into the box and take the First In Last Out method. Because then we can take the item which was the last one put into the box. Likewise in a recursive method, each time we call that recursive function, we store data in the stack. When the base condition is true, it passes the answer to the calling function and repeats until the stack is empty. When passing answers to the calling recursive function, stack taking out that data to outside and so on. So yes, recursion uses more memory compared to iteration.
I think this would be clear for you. If it isn't, try that link when you are coding. You can get an idea of what happens in the code: https://pythontutor.com/