views:

180

answers:

5

I've come across some code that surrounds the return value from a method/function in parentheses.

What does that do?

The code I saw took an image, resized it and then returned it.

- (UIImage *)resizeImage:(UIImage *)image
{
    //
    // some fascinating, but irrelevant, resizing code here
    //

    return (image);
}
+3  A: 

Nothing. It is completely useless.

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

Tomas
You mean, no operator will have *lower* "precedence" than `return`. Which isn't an operator...
Steve Jessop
Thanks, corrected
Tomas
+4  A: 

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 ).

John Bode
+1  A: 

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

sskuce
`//` comments are not really a style choice, because they are allowed since C99. If you program against the old standard, you better don't use them if you want to write conforming code.
Secure
I think the compiler I use doesn't even complain when you compile `//` comments with the strict ANSI flag set (C89 is the default, C99 isn't really supported)
Nick T
@Nick: Often the ANSI semantics flag is separate from the all-warnings flag.
Potatoswatter
A: 

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;
Splashdust
Don't you need `->` in place of `.`? And a `;` at the end?
ArunSaha
`x->y` is synonymous with `(*x).y`, so you would be double dereferencing. I'm not sure how casting would work with member access, but `((SomeClass)ptr)->someproperty` might work
Nick T
In Obj-C, you can use the dot syntax when accessing properties. So [code]((SomeClass *)ptr).someproperty;[/code] is equivalent to [code][(SomeClass *)ptr someproperty];[/code]And yep, I forgot the semi-colon :)
Splashdust
A: 

Nothing (as stated in the other answers. One thing I’d like to add though:)

In the example you posted it’s probably to indicate that a pointer is returned and the star does not belong to the function name. It makes it more readable on what is returned. (subjective of course)

Kissaki