1

Here's my code. I expect it to return 5 because of the incremental by one. The problem is if x >= 5, then how can I return the value to the main fuction? There should be only 1 return in a function.

int addup(int x)
{
    if (x < 5)
    {
        std::cout<< x++;
        addup(x);
    }
    else return x;
}

int main()
{
    using namespace std;
    int x = 0;
    cout << "And x is:" << addup(x) << endl;

    cin.clear();
    cin.ignore(32767, '\n'); 
    cin.get(); 
    return 0;
}

4 Answers 4

4

You are missing a return from the recursive call.

Replace

addup(x);

by

return addup(x);

Without the return in that line, the function does just falls off at the end, and is subject to undefined behavior.

A good compiler will warn you of the missing return statement in the if block. When compiled with -Wall, I get the following warning from g++:

socc.cc: In function ‘int addup(int)’:
socc.cc:11:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
Sign up to request clarification or add additional context in comments.

Comments

2

You should always return a value during the recursion:

int addup(int x)
{
    if (x < 5)
    {
        std::cout << x++;
        return addup(x);
    }
    else return x;
}

In some languages (such as Java), your original code would result in a compiler error as it does not always return a value. In your C++ compiler this was apparently allowed, though both flows of your if-else should return a value as good practice.

Comments

0

Well, to understand the reason of having this return in recursion you can think of basics of function call/stack-frame:

We need return statements to pass a value back to the immediate caller of the current function's call-frame. This immediate caller can be another invocation of that same function in case of recursions.

If the return value of a function you called (be it recursive or normal) is not used, either that return value gets discarded or it is a error in many languages. In some cases, the return value of the last function call gets automatically re-used as the return value of the current function invocation. That is why you may see undefined behavior and t prevent this some intelligent compilers do warn about it.

Recursion calls should have:

  1. Base case: Some condition for which we have a solution and a return value. There can be more than one base cases.

  2. Algorithm or logic to get closer to our base case (i.e breaking down original problem to simpler problem).

  3. Recursive call which passes the simpler problem back into the function.

So in this case, your base case is (x>=5), logic to get closer to the base case is (x++) and recursive call is addup with argument x. But you do not save the return value here which may be discarded or lost and thus can cause further issues and thus it should be dealt properly as: return addup(x);

I hope it makes sense now.

Comments

0

You forgot to add a return, that's all:

...
if (x < 5)
{
    std::cout<< x++;
    return addup(x);
//  ^^^^^^
}
...

Comments

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.