0

I created a recursive function in c++ and I want that after executing all recursive calls when I return to the first recursive call of my function then only I execute a specific statement which is inside the function. Basically, I want some mechanism by which I can execute a statement only once but when the control returns to the first recursive call. I tried static but it actually creates a single copy for all recursive function so each time the control encounter that specific line in my code it get changed and I don't want that. I want it once but when the control returns to the first recursive call.

Is there any keyword or mechanism to handle this behavior or I have to do this only via some conditional logic?

3
  • 2
    Having a look at your code will help us to help you better Commented Oct 16, 2018 at 8:40
  • 1
    Possible duplicate of How do I track recursion depth? Commented Oct 16, 2018 at 8:42
  • I don't think it's a dupe because that's only one solution. Unfortunately the question is so broad, but that answer surely doesn't cover all of them. Commented Oct 16, 2018 at 9:00

2 Answers 2

2

Why not just

void real_recursive_call() {
    // do recursive work
}

void recursive_call() {
    real_recursive_call();
    do_something();
}

int main() {
    recursive_call();
}
Sign up to request clarification or add additional context in comments.

3 Comments

for the same work, I don't want to add extra function because this adds 2 functions for all my recursive functions and my code becomes messy and even loses readability to some extent.
Having a "startup" function for a recursive algorithm is quite common.
Having a "startup" function also allows you to keep the public surface function free of any state that you pass around, and means that callers don't need to know what that state is.
0

What about this:

void Plus(int Val, bool bFirstCall = true)
{
    // Show the current depth for debugging purposes:
    std::cout << "current val: " << Val << std::endl;

    // Increase the depth and call the recursive function with bFirstCall == false explicitly
    if (++Val < 100) Plus(Val, false);

    if (bFirstCall)
    {
        std::cout << "This is the first call" << std::endl;
    }
}

You can make this more elegant by having the recursive method private, and having a public method that calls this one with bFirstCall = true.

3 Comments

This will break if, for some reason, the first call of Plus() is called with the second argument false. There is nothing to prevent that.
Also, doesn't this increase the cost of the recursive call since the stack for each call has to be bigger?
no extra parameter because I don't want to edit calls but instead it will be better if a variable is used inside the body

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.