# Multiple Returns: Am I wrong?

• 05-09-2008
Multiple Returns: Am I wrong?
I was doing another K&R question and again my idea was quite different but I thought I would run it by you bright people.
The exercise is to rewrite an if else conditional using ?:

I realize that I don't need parentheses around the conditional as ?: precedence is low but K&R says it is good practice.
The solutions for this exercise that I have seen are slightly but significantly different using only one return at the beginning of the function and I understand those solutions but I was wondering if mine is wrong.

Original:
Code:

```int lower(int c) {         if (c >= 'A' && c <= 'Z')                 return c + 'a' - 'A';         else                 return c; }```
My idea.
Code:

```int lower(int c) {         (c >= 'A' && c <= 'Z') ? return c + 'a' - 'A' : return c; }```
• 05-09-2008
IceDane
Quote:

I was doing another K&R question and again my idea was quite different but I thought I would run it by you bright people.
The exercise is to rewrite an if else conditional using ?:

I realize that I don't need parentheses around the conditional as ?: precedence is low but K&R says it is good practice.
The solutions for this exercise that I have seen are slightly but significantly different using only one return at the beginning of the function and I understand those solutions but I was wondering if mine is wrong.

Original:
Code:

```int lower(int c) {         if (c >= 'A' && c <= 'Z')                 return c + 'a' - 'A';         else                 return c; }```
My idea.
Code:

```int lower(int c) {         (c >= 'A' && c <= 'Z') ? return c + 'a' - 'A' : return c; }```

I assume you mean:

Code:

`    return (c >= 'A' && c <= 'Z') ? c + 'a' - 'A' : c;`
In which case - no, yours isn't incorrect, and they probably look the same when the compiler's compiled and optimized the code anyway. However, you might want to look at the difference and see why the ternary operator is so nice - It can be put into assignments/function calls and so on - it doesn't need to be 'on a line by itself'.

Although, you should be wary of using it too much, especially with complex if statements. They probably look exactly the same when compiled, but the ternary operator is often harder to read/dissect.
• 05-09-2008