ryan
ryan

Reputation: 645

Does a return statement stop the recursive stack from continuing?

I'm coding up a search_key algorithm for a binary search tree and am having issues. I'm traversing the binary search tree and comparing the nodes using an in-order traversal algorithm using the basic recursion technique. However, when the conditional hits (search_key == node[x]->value) and I return the node, it seems as if it still continues executing code even after I return the node and it affects the final output. Does the recursion stack continue even if it hits a return value? Is there a way to fix this?

Upvotes: 0

Views: 2209

Answers (2)

morr36
morr36

Reputation: 53

You should post the snippet of code you're talking about.

Aside from that, if you've called your recursive function a given number of times, returning from the function would return you to the previous call of that function. A return statement won't stop all the prior recursive calls made from executing.

Upvotes: 2

Mats Petersson
Mats Petersson

Reputation: 129454

return will return from the current function, but of course where you return to, in a recursive situation, is the level below, so you may need to check the result and decide what to do, and not continue searching the other side of a tree, for example.

Upvotes: 4

Related Questions