Exiting a recursive function at the topmost level in C ++
The question is, how did you get there? Which algorithm buries you deeply in recursion without being able to get out of it?
Any recursive function must have a way to terminate the recursion, it only recurses if the condition is either true or false. When this fails, the recursion ends and the function returns instead of recursing deeper.
Why don't you just end up recursion this way, going back through all the levels?
If you're desperate, exception is the way to go, but it's (rightfully so IMO) frowned upon.
a source to share
No, you cannot reverse your recursion and go straight back to your main (). If your recursive function does not do different work after the recursive call, you would effectively do the same thing. I recommend restructuring a recursive function. A description of why you want to break out of recursion early on will also be helpful.
a source to share