I wrote the absolute function using ternary operator as follows
int abs(int a) {
a >=0 ? return a : return -a;
}
I get the following er
?:
is an operator that takes three expressions and evaluates them in some way to produce a result. return a
is not an expression (it's a statement), so your first form doesn't work. It's the same as you can't put return
in the arguments of other operators: return a + return b
will also not work.
If you want the returns in the separate branches, use if
instead:
if (a >=0)
return a;
else
return -a;