Recursion-related problem

K

kelvSYC

I have a method that says something like this:

void foo(int b) {
// Do stuff and calculate the value of condition and anotherInt
if (condition) {
foo(anotherInt);
}
}

How can I change foo so that the inner foo is called only when the
outer foo is completed (ie. only one foo method is running)?
 
M

Michael Borgwardt

kelvSYC said:
void foo(int b) {
// Do stuff and calculate the value of condition and anotherInt
if (condition) {
foo(anotherInt);
}
}

How can I change foo so that the inner foo is called only when the
outer foo is completed (ie. only one foo method is running)?

By elimination the recursion. How that can be done (and whether it can be done
at all) depends on the algorithm in question.

Basically, you need to replace the recursion with a loop, initialize the data
beforehand and then run the loop until the termination condition is met.
 
E

Eric Sosman

kelvSYC said:
I have a method that says something like this:

void foo(int b) {
// Do stuff and calculate the value of condition and anotherInt
if (condition) {
foo(anotherInt);
}
}

How can I change foo so that the inner foo is called only when the
outer foo is completed (ie. only one foo method is running)?

void foo(int b) {
do {
// Do stuff and calculate the value of condition and anotherInt
b = anotherInt;
} while (! condition);
}
 

Ask a Question

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

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top