473,842 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13227
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******@discu ssions.microsof t.com> wrote in message
news:5E******** *************** ***********@mic rosoft.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(va lue As Object, theCurrentLevel ) As
Boolean
For Each item In theCurrentLevel
If item = value Then Return True
If RecusiveFind(va lue, item.Children) Then Return True
' keep looking at the current level
Next
Return False
End Function
"Bill Borg" <Bi******@discu ssions.microsof t.com> wrote in message
news:5E******** *************** ***********@mic rosoft.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****@microso ft.com> wrote in message
news:Og******** *****@TK2MSFTNG P09.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******@discu ssions.microsof t.com> wrote in message
news:5E******** *************** ***********@mic rosoft.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******** ********@TK2MSF TNGP10.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****@microso ft.com> wrote in message
news:Og******** *****@TK2MSFTNG P09.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******@discu ssions.microsof t.com> wrote in message
news:5E******** *************** ***********@mic rosoft.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******** ********@TK2MSF TNGP10.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****@microso ft.com> wrote in message
news:Og******** *****@TK2MSFTNG P09.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******@discu ssions.microsof t.com> wrote in message
news:5E******** *************** ***********@mic rosoft.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****@microso ft.com> wrote in message
news:eL******** *******@TK2MSFT NGP10.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******** ********@TK2MSF TNGP10.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****@microso ft.com> wrote in message
news:Og******** *****@TK2MSFTNG P09.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******@discu ssions.microsof t.com> wrote in message
news:5E******** *************** ***********@mic rosoft.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.c o.uk...
You could kill the whole stack by throwing an exception :-)

--
Jonathan Bailey.

"Imran Koradia" <no****@microso ft.com> wrote in message
news:eL******** *******@TK2MSFT NGP10.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******** ********@TK2MSF TNGP10.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****@microso ft.com> wrote in message
news:Og******** *****@TK2MSFTNG P09.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******@discu ssions.microsof t.com> wrote in message
> news:5E******** *************** ***********@mic rosoft.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****@microso ft.com> wrote in message
news:%2******** ********@tk2msf tngp13.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.c o.uk...
You could kill the whole stack by throwing an exception :-)

--
Jonathan Bailey.

"Imran Koradia" <no****@microso ft.com> wrote in message
news:eL******** *******@TK2MSFT NGP10.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******** ********@TK2MSF TNGP10.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****@microso ft.com> wrote in message
> news:Og******** *****@TK2MSFTNG P09.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******@discu ssions.microsof t.com> wrote in message
> > news:5E******** *************** ***********@mic rosoft.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.c o.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****@microso ft.com> wrote in message
news:%2******** ********@tk2msf tngp13.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.c o.uk...
You could kill the whole stack by throwing an exception :-)

--
Jonathan Bailey.

"Imran Koradia" <no****@microso ft.com> wrote in message
news:eL******** *******@TK2MSFT NGP10.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******** ********@TK2MSF TNGP10.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****@microso ft.com> wrote in message
> > news:Og******** *****@TK2MSFTNG P09.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******@discu ssions.microsof t.com> wrote in message > > > news:5E******** *************** ***********@mic rosoft.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
3115
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 that Numerology program I wrote years ago in VB. There are times I am totally stuck (for instance, I just had an idea to put the numerical values of the alphabet and months of the year in a dictionary located in a function. Then, I can import the...
2
5302
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 supposed to traverse sibling nodes in each iteration, and for each sibling node, it calls itself again, to see if there are child nodes of the current sibling.
2
2895
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 a sequence of spaces; recursive function 2 uses input to display ascending sequence of digits; likewise, recursive function 3 uses input to display descending sequence of digits. I have not followed the instructions completely regarding the...
4
9059
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. However, it is not as straightforward to determine the base address for my stack space. The approach I have taken is to save the address of an automatic variable in main( ), and assume this is a fairly good indicator of my base address. Then, I can...
9
16854
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 type='text/javascript'> function myFunc(lev) { // if (lev) return myFunc(lev-1); var aStack=; nextFunc = arguments.callee;
0
1842
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 different ways and none of the versions i've made works exactly as it should. I have an array called $PageArray which contains a sorted list of all pages in my application. Im trying to create a recursive function(It dosn't need to be recursive if...
2
1816
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 your characters into that save file. Anyhow, I want to extract a couple of lines of it and save it into a database and I need help on figuring out a good way of reading the file. The problem is that the file can look pretty different depending...
9
2644
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 object.The object is triangulated. The rays can undergo multiple reflections, diffractions etc of the same object i.e. a ray hits a surface of the object, undergoes reflection resulting in a reflected ray which can again hit a surface, corner or edge...
0
10944
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10610
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10313
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9453
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7855
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5696
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5884
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4499
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4089
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.