Void Recursion

Hi fellas, could somebody answer how can i END a void recursion? I said END, not return to the previous call, I want something similar to "break" in loops but didn't find it in the web :/
The only way i can think of to do this is to use a thread for the initial call and terminate the thread.
Last edited on
What's meant to happen after you END the void recursion, given that it doesn't carry on with the calling code?
You could throw an exception, although this sounds like you're looking to a solution to a non-problem.
Last edited on
Recursion is equivalent to iteration, so unless your recursive function is poorly designed, there's no reason why simply returning to the caller isn't a suitable way of stopping the recursion.

1
2
3
4
5
6
void f(int i){
    if (!i)
        return;
    std::cout << i << std::endl;
    f(i - 1);
}
Is this question based on the belief that if you return, you have to return something?

Much like this question: https://stackoverflow.com/questions/346613/how-do-you-exit-from-a-void-function-in-c
No, OP wants to do something akin to breaking out of a deeply-nested loop.

It is an incorrect analogy. You cannot avoid passing back up through the call stack (unless you have tail-call recursion, which good C++ compilers will do for you if you design a TCO function).


[edit]
Well, there are a lot of ways to do it, actually, but like cire said, it is a non-problem.
Why do you think this matters?
Last edited on
Oh, I get it. The OP is asking to jump all the way through the stack back to the last call that wasn't named the same as the current one; all the way back to the first call to the function that is calling itself.

Yes, can't help but think this sounds like solving a problem that shouldn't (or doesn't really) exist.
Registered users can post here. Sign in or register to post.