Phuran
Phuran

Reputation: 23

Function returning wrong value in C++

I am trying to call a function to return the sum of an input number but it is only returning 0.

The for loop calculates the sum correctly when applied in the main, without calling the function. Nevertheless, when I call the function it returns a wrong value.

#include <iostream>
using namespace std;

int EvenOrOdd(int Number, int Value)
{
    if (Number % 2 == 0)
        Value = 1;
    return(Value);
}

int CalculateSum(int Number, int Sum)
{
    for (int i = 1; i <= Number; i++)
        Sum = Sum + i;
    return(Sum)
}

void main()
{
    int Number = 0, Value = 0, Sum = 0;

    cout << "Please enter a positive integer: ";
    cin >> Number;

    while (Number >= 1 && Number <= 100)
    {

        EvenOrOdd(Number, Value);
        if (Value == 1)
            cout << Number << " is Even";
        else
            cout << Number << " is Odd";

        CalculateSum(Number, Sum);
        cout << "\n" << Sum << " is the sum of the first " << Number;

        cout << "\nPlease enter a positive integer: ";
        cin >> Number;
    }
}

Upvotes: 1

Views: 3132

Answers (2)

David C. Rankin
David C. Rankin

Reputation: 84642

You never make use of return(Value);. If you want to use the new value without making use of the return you must pass a reference, e.g. int EvenOrOdd(int Number, int& Value) (or pass a normal old pointer). Likewise with CalculateSum(Number, Sum);

Why? Unless you pass a reference or a pointer, the parameters are passed-by-value and the function receives a copy of the original variable. Any changes made to the copy within the function are lost when the function returns. (e.g. the caller has no way of seeing the changes).

You address the problem in one of two ways. (1) make use of the return to obtain the value being returned by the function, e.g.

Value = EvenOrOdd(Number, Value);

or (2) pass a reference to value, e.g.

int EvenOrOdd(int Number, int& Value)
{
    if (Number % 2 == 0)
        Value = 1;
    return(Value);
}

By passing a reference, any changes made to Value in the function are automatically made to the original variable back in the caller because the reference is just an alias for the original variable. The same applies to your use of Sum in CalculateSum(Number, Sum);

Example Using References

#include <iostream>
using namespace std;

int EvenOrOdd(int Number, int& Value)
{
    if (Number % 2 == 0)
        Value = 1;
    return(Value);
}

int CalculateSum(int Number, int& Sum)
{
    for (int i = 1; i <= Number; i++)
        Sum = Sum + i;
    return(Sum);
}

int main (void)
{
    int Number = 0, Value = 0, Sum = 0;

    cout << "Please enter a positive integer: ";
    cin >> Number;

    while (Number >= 1 && Number <= 100)
    {

        EvenOrOdd(Number, Value);
        if (Value == 1)
            cout << Number << " is Even";
        else
            cout << Number << " is Odd";

        CalculateSum(Number, Sum);
        cout << "\n" << Sum << " is the sum of the first " << Number;

        cout << "\nPlease enter a positive integer: ";
        cin >> Number;
    }
}

(note: I have used a conforming invocation of main() above -- see below)

You are also using a non-conforming invocation of main(). The conforming invocations are int main (void) or int main (int argc, char **argv) (though argc and argv can be named anything. If you are on a non-conforming embedded system that requires void main() then you are OK, otherwise you need to fix it.

Upvotes: 1

alex067
alex067

Reputation: 3301

You are receiving 0 for the sum because you are not assigning the return value to anything.

Try this instead:

int sum = CalculateSum(Number, Sum);

So now you are holding the return value from CalculateSum to a variable sum, which you can use to output to the user.

Upvotes: 1

Related Questions