Need to break out of recursive function

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I call a function recursively to find an item that exists *anywhere* down
the chain. Let's say I find it five layers deep. Now I've got what I need and
want to break out of that whole stack and continue execution at the point of
the initial call. Is that possible?

Thanks,

Bill
 
Do you mean skip the whole call stack? If that's the case, then AFAIK, you
can't do that. You'll have to follow the call stack back to the point of the
initial call. I believe the reason for this is the for each call to the
recursive method, the current stack frame within the runtime stack has a
pointer only to the previous stack frame from which this call was initiated
and knows nothing about any stack frames before that. As soon as the current
call returns, it returns to the address of the previous stack frame from
which this call was made.

hope that helps..
Imran.
 
Bill,
Normally I have the function return a "found" indicator (a Boolean), when I
call it recursively if I find the item I exit the current item.

Something like
Public Function RecusiveFind(value As Object, theCurrentLevel) As
Boolean
For Each item In theCurrentLevel
If item = value Then Return True
If RecusiveFind(value, item.Children) Then Return True
' keep looking at the current level
Next
Return False
End Function
 
Hi Imran,

You can add a global variable. Set it equal to False. Once you hit your
finish point set it to True and Exit Function. Then in the function,
directly after where it calls itself check this variable. If it is True
then Exit Function which will move you right back up the stack and out.
I've never tried using a static variable. I have no idea if that would
work, hmmmm I'll have to try it. Good luck! Ken.
 
Why a global variable? Couldn't you just pass your boolean variable as one
of the parameters of the recursive function. In fact, isn't this how you
create a recursive function???

Greg
 
Right - I understand that. But you're still going to be checking that
variable for each recursive call that you made. Ofcourse, you can avoid the
code after the recursive call within the method. But you're still going to
pass through the entire call stack back to the initial call. There's no way
you can skip the call stack. Or am I missing something?

Imran.
 
Well - even when an exception is thrown, the runtime searches up the call
stack to look for an appropriate exception handler which means its still
going to be travelling up the call stack. However, I'm not sure how the
runtime handles exceptions for recursive procedures - is it even aware that
its a recursive call? If so, it could just check once whether the procedure
has an exception handler and if not, it could skip the entire stack sequence
for the recursion and jump right back to the initial calling method. But I
doubt that..I could be wrong though..

Imran.
 
I was joking of course :-)
I cant find in the MS website where exceptions are defined but I expect vb
creates a try,catch block around any function which might throw one which
then passes it to the previous caller.
It just 'looks like' you are skipping up the stack in the source code.
 
lol :)
yeah - I couldn't find anything either but considering how exception
handling works in java, I bet that's how it works.

Imran.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top