return statement in ternary operator c++

前端 未结 5 1445
不知归路
不知归路 2020-12-02 23:01

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

5条回答
  •  攒了一身酷
    2020-12-02 23:24

    Your syntax is incorrect. It should be

    if (a >=0)
        return a;
    else
        return -a;
    

    or the way you wanted it:

    return a >=0 ? a : -a;
    

提交回复
热议问题