This program works and im trying dissect in and understand. But for love of God I cannot figure out why the value returned is 1214 and not -1. I'm sure returning the abs is the cause, but I can't figure out why. A brief explanation will suffice or a link to where I can read up on it.
It was " int diff = abs (x - y)" which would always output -1. But after changing it to the above code it outputted the correct value. I'm trying to understand WHY that is.
//This function finds the difference of x & y
// and returns an absolute value, so it won't be negative.
int difference(constint x, constint y)
{
return abs(x - y);
}
int main(int argc, constchar *argv[])
{
//This calls the difference function with values x = 24, y = 1238
// 24 - 1238 = -1214
// abs(-1214) = 1214. Therefore it should return 1214 and not -1
cout << difference(24, 1238) << endl;
return 0;
}