Ok I'm using the GCC compiler in C . I have a tree style of menu that has been designated function "Manager". I want to return to the top of the tree so basically what I do is at some point from within the "Manager" function I make the call to "Manager" again.

void Manager()
{
...
...
Manager();
...
return;
}

Now I'm wondering if this is good programming practice or if this will cause any bugs in my program because I have functions that do not reach the "return" point or what is the end of the code.

I have noticed a few bugs in my program and I'm wondering if this is the cause because it seems it's not a consistently occurring bug.

Ok I'm using the GCC compiler in C . I have a tree style of menu that has been designated function "Manager". I want to return to the top of the tree so basically what I do is at some point from within the "Manager" function I make the call to "Manager" again.

void Manager()
{
...
...
Manager();
...
return;
}

Now I'm wondering if this is good programming practice or if this will cause any bugs in my program because I have functions that do not reach the "return" point or what is the end of the code.

I have noticed a few bugs in my program and I'm wondering if this is the cause because it seems it's not a consistently occurring bug.

What you are doing (calling a function from within that function) is called "recursion" and it's done all the time and sometimes it is good programming practice and sometimes it is not. One thing to worry about is making sure that you are able to eventually get out of that function. So what you want to avoid is something like this:

void Manager ()
{
    cout << "I am in an infinite loop!\n";
    Manager ();
    return;
}

Somehow, under some conditions, you must make sure that there is some case where you go through the Manager function and you don't call it again. Often it is something like this:

void Manager ()
{
    // code
    if (condition)
        return;

    Manager ();
}

condition could be all sorts of things, but it allows at least one case where Manager () is not called so you don't end up in an infinite loop.

Yeah, that's pretty bad.

If you want to loop, use a loop, not a recursive function call.
If you go round enough times, you may end up using all your stack space.

Try

void Manager ( ) {
  do {
    // some code to repeat a number of times
  } while ( someCondition );
}

Oh, and figure out how to use code tags, like this example does.

Well it's not necessarily bad.

If you are sure you are only going to recurse N-times, with N being.. say, below 2000, it's okay to do it recursive. I somewhere read: "Never hire a developer who computes the factorial using Recursion."

What it says it that you should avoid recursion if it's really simple to implement a non-recursive way. However, recursion is often more a more natural approach to solving problems, so feel free to use it, imho.

Ok thanks guys for your input. I originally did plan this out with a do/while loop but went towards the recursion side. Realistically I can only see the loop being done maybe 20 times before exiting. In the tree I do have an option to exit the loop and will add in more exit points as I build on the code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.