Thread: If, else in a function?

1. If, else in a function?

So I am trying to do a simple subtraction problem where the larger number would go first so the difference can be a positive or zero, but I am stump. If I just remove the if and else with either x - y or y - x it would work. It is something in my function from what it seems.

Code:
```#include <iostream>

using namespace std;

int sub ( int x, int y );

int main()
{
int x;
int y;

cout<<"We will subtract two numbers and it will always be positive or zero.";
cin.ignore();
cin>> x >> y;
cin.ignore();
cout<<"The difference is "<< sub ( x, y ) <<"\n";
cin.get();
}

int sub ( int x, int y )
{
if ( x < y ) {
return y - x;
}
else ( x > y ) {
return x - y;
}
}```

2. Else should not have a condition. Just write else, followed by the curly brace.

3. It's either
else if (condition)
Or
else

4. Code:
```int sub ( int x, int y )
{
if ( x < y ) {
return y - x;
}
else ( x > y ) {
return x - y;
}
}```
Or just replace "else" with "if".

5. No, that's not good, because this is a if or else if type of logic.

6. Thanks guys, else if works, but I did it before and it didn't work. I must have missed a semi colon before.

7. What about the case where neither if condition is met (for example, if I input 5 and 5)?

Don't forget to come up with a solution for that (hopefully you can figure it out before somebody gives it to you).

>> No, that's not good, because this is a if or else if type of logic.
Since the blocks contain a return statement, audinue's suggestion is logically equivalent to an if/else if. Whether it's better or worse for readability is probably a debate for another thread.

8. >> What about the case where neither if condition is met (for example, if I input 5 and 5)?

I did this, seems to work, I don't know if there is any way around it.

Code:
```int sub ( int x, int y )
{
if ( x < y ) {
return y - x;
}
else if ( x > y ) {
return x - y;
}

else  {
return 0;
}

}```
http://www.mediafire.com/?myynmdyqmmz

9. >> I don't know if there is any way around it.
There are other ways, but that's just fine.

10. Originally Posted by Daved
>> No, that's not good, because this is a if or else if type of logic.
Since the blocks contain a return statement, audinue's suggestion is logically equivalent to an if/else if. Whether it's better or worse for readability is probably a debate for another thread.
I know it is and in this case it might be fine, but in others it might not, since in if, all ifs are executed and checked, but in else/if, only the if/else ifs down to the point where the condition is true is evaluated.
Therefore, I suggest separating if and else if logic.

11. What about the case where neither if condition is met (for example, if I input 5 and 5)?
Wouldn't this be handled automatically if you didn't check for the second condition (if x is not smaller than y, then it is greater or equal)?

Code:
```if (x < y) {
return y - x;
}
else {
return x - y;
}```

12. Wouldn't this be handled automatically if you didn't check for the second condition (if x is not smaller than y, then it is greater or equal)?
No actually, it comes out 5 instead of 0.

13. Originally Posted by ShinobiRAGE
No actually, it comes out 5 instead of 0.
There must be some error in your code then, because it should definitely return 0;

For example -

Code:
```if (x < y) {
return y - x;
}
else {
return x - y;
}```
Lets plug in 5 and see,

Code:
```if (5 < 5) {//False so skip
return y - x;
}
else {//True
return 5 - 5;//should be 0
}```

14. also
Code:
```int sub ( int x, int y )
{
if ( x < y ) {
return y - x;
}
return x - y;
}```

15. >> Wouldn't this be handled automatically if you didn't check for the second condition?

Yes, and that's how I would do it (or even something closer to ಠ_ಠ's solution). But I think the solution ShinobiRAGE came up with is fine and it is at least original.