What is the intention of putting return values in parentheses in C/Objective-C?

耗尽温柔 提交于 2019-12-05 12:36:45

At least as far as C is concerned, it makes no difference. The parens aren't necessary, but they don't change the meaning of the return statement. The grammar of the return statement is

return-statement:
    return expressionopt ;

and one of the productions of the expression non-terminal is a parenthesized-expression, or ( expression ).

Nothing. It is completely useless.

It overrides operator precedence, yet no operator will have a lower precedence than "return".

The short answer is simply that it's a style choice, like using "/* comments */" instead of "//comments"

In your case it is equivalent of typing the return without the parentheses. Typically, you would use parentheses for type casting, or if you want to treat an expression as an independent block.

For example:

// This is an untyped pointer to an instance of SomeClass
void* ptr = instance;

// In order to let the compiler know that ptr is an instance of SomeClass
// we cast it, and then we put the cast in parentheses to be able to access
// a property on the result of the cast.
return ((SomeClass *)ptr).someproperty;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!