473,221 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

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
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
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
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
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
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
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
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
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: <script...
0
by: Michael L | last post by:
Hi Guys(I apologize for the lengty post - Im trying to explain it as best i can) I've been cracking my head on this one for the past 24+ hours and i have tried creating the function in ten...
2
by: Anders B | last post by:
I want to make a program that reads the content of a LUA array save file.. More precicely a save file from a World of Warcraft plugin called CharacterProfiler, which dumps alot of information about...
9
by: pereges | last post by:
Hello I need some ideas for designing a recursive function for my ray tracing program. The idea behind ray tracing is to follow the electromagnetic rays from the source, as they hit the...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.