Question

I made a program that calls this function. I know this because "Int Strength has been called" appears in the output box. However, it will not change the values that I tell it to do. I want it to get integer values from main(), then use them and return the new values. I am using a header file that only contains "int strength(int a, int s, int i)"

int strength(int a, int s, int i)
{
    using namespace std;
    cout << "Int Strength has been called" << endl;
    a = a + i;
    s = s - i;
    return a;
    return s;
}
Was it helpful?

Solution

Multiple errors. Firstly, if you want the arguments to be modified (more precisely, the modification being effective out of the scope of the function), you have to pass the values by reference:

int strength(int &a, int &s, int &i)

Second, you seem to be concerned about return a; return s; returning two values. It doesn't - the very first return encountered exits the function immediately.

OTHER TIPS

The values only change within the function. Variables are passed by value not reference. Use references as the parameters.

 int strength(int& a, int& s, int& i)

You're passing by value. You need to pass a pointer to the memory allocated in the caller that contains the data you wish to modify.

void strength(int *a, int *s, int i)
{
    using namespace std;
    cout << "Int Strength has been called" << endl;
    *a += i;
    *s -= i;
}

Then call it thusly:

a = 1;
s = 2;
i = 3;

strength(&a, &s, i);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top