By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,508 Members | 1,640 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,508 IT Pros & Developers. It's quick & easy.

Ternary operator ((a>b)?a:b) inside return statement?

P: 1
Is it possible to write "return" function inside a conditional operator as shown below?

a>b? return a: return b;
May 24 '07 #1
Share this Question
Share on Google+
6 Replies


Savage
Expert 100+
P: 1,764
Is it possible to write "return" function inside a conditional operator as shown below?

a>b? return a: return b;
Yes,but it would be:

return(a>b)?a:b;

Savage
May 24 '07 #2

sicarie
Expert Mod 2.5K+
P: 4,677
I've changed the title of the thread to better describe the issue - please let me know if you don't think it does (and what you want it to be).
May 24 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
Is it possible to write "return" function inside a conditional operator as shown below?

a>b? return a: return b;
No. If you try this it won't compile.

The ternary operator requires expressions but not code. You can, however, call a function because the function becomes an instance of its return type so you get the return value from the operator.
May 24 '07 #4

Expert 100+
P: 181
No. If you try this it won't compile.

The ternary operator requires expressions but not code. You can, however, call a function because the function becomes an instance of its return type so you get the return value from the operator.
But most of the book defines terinary operator as
Condition ? expression 1: exression 2;

My doubt : Is return statement is a expression or not.
May 24 '07 #5

Expert 10K+
P: 11,448
how about a>b?return a; return b;
That won't work; 'return' is a statement and the ternary operator ?: wants
expressions. It just won't compile.

kind regards,

Jos
May 24 '07 #6

Expert 100+
P: 181
That won't work; 'return' is a statement and the ternary operator ?: wants
expressions. It just won't compile.

kind regards,

Jos
Oh i got it, i think i really need a long break during this weekend. :)
May 24 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.