By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,702 Members | 1,432 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,702 IT Pros & Developers. It's quick & easy.

Need to break out of recursive function

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
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 #2

P: n/a
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

P: n/a
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 #4

P: n/a
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
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 #5

P: n/a
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 #6

P: n/a
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 #7

P: n/a
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 #8

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.