473,221 Members | 1,908 Online

# Need to break out of recursive function

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
Nov 21 '05 #1
9 13066
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

hope that helps..
Imran.

"Bill Borg" <Bi******@discussions.microsoft.com> wrote in message
news:5E**********************************@microsof t.com...
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

Nov 21 '05 #2
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
"Bill Borg" <Bi******@discussions.microsoft.com> wrote in message
news:5E**********************************@microsof t.com...
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

Nov 21 '05 #3
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.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight

"Imran Koradia" <no****@microsoft.com> wrote in message
news:Og*************@TK2MSFTNGP09.phx.gbl...
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

hope that helps..
Imran.

"Bill Borg" <Bi******@discussions.microsoft.com> wrote in message
news:5E**********************************@microsof t.com...
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

Nov 21 '05 #4
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

"Ken Dopierala Jr." <kd*********@wi.rr.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight

"Imran Koradia" <no****@microsoft.com> wrote in message
news:Og*************@TK2MSFTNGP09.phx.gbl...
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

hope that helps..
Imran.

"Bill Borg" <Bi******@discussions.microsoft.com> wrote in message
news:5E**********************************@microsof t.com...
> 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

Nov 21 '05 #5
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.

"Ken Dopierala Jr." <kd*********@wi.rr.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight

"Imran Koradia" <no****@microsoft.com> wrote in message
news:Og*************@TK2MSFTNGP09.phx.gbl...
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

hope that helps..
Imran.

"Bill Borg" <Bi******@discussions.microsoft.com> wrote in message
news:5E**********************************@microsof t.com...
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

Nov 21 '05 #6
You could kill the whole stack by throwing an exception :-)

--
Jonathan Bailey.

"Imran Koradia" <no****@microsoft.com> wrote in message
news:eL***************@TK2MSFTNGP10.phx.gbl...
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.

"Ken Dopierala Jr." <kd*********@wi.rr.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight

"Imran Koradia" <no****@microsoft.com> wrote in message
news:Og*************@TK2MSFTNGP09.phx.gbl...
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 Borg" <Bi******@discussions.microsoft.com> wrote in message
news:5E**********************************@microsof t.com...
> 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

Nov 21 '05 #7
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.

<jb> wrote in message news:41***********************@news.easynet.co.uk. ..
You could kill the whole stack by throwing an exception :-)

--
Jonathan Bailey.

"Imran Koradia" <no****@microsoft.com> wrote in message
news:eL***************@TK2MSFTNGP10.phx.gbl...
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.

"Ken Dopierala Jr." <kd*********@wi.rr.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight

"Imran Koradia" <no****@microsoft.com> wrote in message
news:Og*************@TK2MSFTNGP09.phx.gbl...
> 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 Borg" <Bi******@discussions.microsoft.com> wrote in message
> news:5E**********************************@microsof t.com...
> > 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
>
>

Nov 21 '05 #8
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.

--
Jonathan Bailey.

"Imran Koradia" <no****@microsoft.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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.

<jb> wrote in message news:41***********************@news.easynet.co.uk. ..
You could kill the whole stack by throwing an exception :-)

--
Jonathan Bailey.

"Imran Koradia" <no****@microsoft.com> wrote in message
news:eL***************@TK2MSFTNGP10.phx.gbl...
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.

"Ken Dopierala Jr." <kd*********@wi.rr.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
> 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.
>
> --
> Ken Dopierala Jr.
> For great ASP.Net web hosting try:
> http://www.webhost4life.com/default.asp?refid=Spinlight
> If you sign up under me and have problems, email me.
>
> "Imran Koradia" <no****@microsoft.com> wrote in message
> news:Og*************@TK2MSFTNGP09.phx.gbl...
> > 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 Borg" <Bi******@discussions.microsoft.com> wrote in message
> > news:5E**********************************@microsof t.com...
> > > 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
> >
> >
>
>

Nov 21 '05 #9
lol :)
yeah - I couldn't find anything either but considering how exception
handling works in java, I bet that's how it works.

Imran.

<jb> wrote in message news:41***********************@news.easynet.co.uk. ..
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.

--
Jonathan Bailey.

"Imran Koradia" <no****@microsoft.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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.

<jb> wrote in message news:41***********************@news.easynet.co.uk. ..
You could kill the whole stack by throwing an exception :-)

--
Jonathan Bailey.

"Imran Koradia" <no****@microsoft.com> wrote in message
news:eL***************@TK2MSFTNGP10.phx.gbl...
> 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.
>
> "Ken Dopierala Jr." <kd*********@wi.rr.com> wrote in message
> news:%2****************@TK2MSFTNGP10.phx.gbl...
> > 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.
> >
> > --
> > Ken Dopierala Jr.
> > For great ASP.Net web hosting try:
> > http://www.webhost4life.com/default.asp?refid=Spinlight
> > If you sign up under me and have problems, email me.
> >
> > "Imran Koradia" <no****@microsoft.com> wrote in message
> > news:Og*************@TK2MSFTNGP09.phx.gbl...
> > > 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 Borg" <Bi******@discussions.microsoft.com> wrote in message > > > news:5E**********************************@microsof t.com...
> > > > 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
> > >
> > >
> >
> >
>
>

Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 10 by: Jeff Wagner | last post by: I am in the process of learning Python (obsessively so). I've been through a few tutorials and read a Python book that was lent to me. I am now trying to put what I've learned to use by rewriting... 2 by: LoserInYourFaceEngineer | last post by: Hello All: I'm having trouble with a recursive function. The function is supposed to identify nested folders in a hierarchical folder structure. The function "searchForFolders()" is... 2 by: | last post by: OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function calls. Recursive function 1 takes input and displays... 4 by: Victor | last post by: Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. ... 9 by: Csaba Gabor | last post by: Inside a function, I'd like to know the call stack. By this I mean that I'd like to know the function that called this one, that one's caller and so on. So I thought to do: