# If, else in a function?

• 08-29-2009
ShinobiRAGE
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();         cout<<"Please input two numbers: ";         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;         } }```
• 08-29-2009
King Mir
Else should not have a condition. Just write else, followed by the curly brace.
• 08-29-2009
Elysia
It's either
else if (condition)
Or
else
• 08-29-2009
audinue
Quote:

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".
• 08-29-2009
Elysia
No, that's not good, because this is a if or else if type of logic.
• 08-29-2009
ShinobiRAGE
Thanks guys, else if works, but I did it before and it didn't work. I must have missed a semi colon before.
• 08-29-2009
Daved
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.
• 08-29-2009
ShinobiRAGE
>> 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
• 08-29-2009
Daved
>> I don't know if there is any way around it.
There are other ways, but that's just fine.
• 08-30-2009
Elysia
Quote:

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.
• 08-30-2009
anon
Quote:

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; }```
• 08-30-2009
ShinobiRAGE
Quote:

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.
• 08-30-2009
Spidey
Quote:

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 }```
• 08-30-2009
ಠ_ಠ
also
Code:

```int sub ( int x, int y ) {         if ( x < y ) {                 return y - x;         }         return x - y; }```
• 08-30-2009
Daved
>> 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.