Can anyone tell me what exactly happens in a Recursion Program.. ??
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A function calls itself over and over, until some condition is met.
A silly example: How to walk 100 steps recursively:
This is what happens:
Note that the same thing could be accomplished with a loop (“iteratively”):
The program flow would be a bit different, yet the result is the same:
It cannot be said whether recursion or iteration is always better. In this example, iteration (loop) is simpler to write and easier to understand than recursion; in other cases (e.g. traversing tree structures), recursion may be a better solution.