views:

938

answers:

8

Quite often in ANSI C code I can see parenthesis sorrounding a single return value.

Like this:-

int foo(int x) {
  if (x)
    return (-1);
  else
    return (0);
}

Why use () around the return value in those cases? Any ideas? I can see no reason for that.

A: 

As often the case when using parenthesis I think that's just for readability (Ruby supports method calls w/o parenthesis enclosing the arguments but recent books and articles advice otherwhise).

Manrico Corazzi
+12  A: 

There really isn't a reason...it's just old convention.

To save space, programmers would often to final math in the return like, like this:

return (x+i*2);

The parenthesis became a habit and it stuck.

Adam Haile
I tend to do it when I return math like that. I dunno, it's like I'm afraid the method will return before the math is done evaluating or something o.O
Wes P
Glad it wasn't just me thinking it's unnecessary. ;-)Parenthesis for setting the priority of are understandable but IMHO a return statement like above should be divided into multiple statements. Increases readability and are less prone to errors. Dare to say no to sausage notation! ;-)
Tooony
A: 

Perhaps this is because with parenthesis it is looking more like a function call, i.e. looking more like the rest of the code?

Or its just something everybody does, just because everybody else does it :-)

Sec
+8  A: 

My personal style is to use parentheses if there is a complex expression; e.g.,

return (a + b);

but to not use them if the expression is a simple term

return a;

I can't say why I do it that way; just something I picked up long ago.

By the way, I think that making it look like a function call, like this:

return(a);  // ugh

is incredibly ugly and just wrong.

Kristopher Johnson
And subjective - I think the space looks worse (which is equally but oppositely subjective).
Jonathan Leffler
+4  A: 

There are a few reasons:

  1. if/while/for/etc. are all control keywords which must have parens. So it often seems natural to always put them on return too.

  2. sizeof is the only other keyword that can either have them or not, except that in some cases you must use parens. So it's easier to get into the habit of always using parens. for sizeof, which implies a logic of: if you can, always do.

  3. case/goto are the only keywords where you never use parens. ... and people tend to think of those as special cases (and like them both to stand out from other control keywords, esp. goto).

James Antill
+4  A: 

A practical, but unlikely, motive is if you put parenthesis around the value, you can define return as a macro, and then insert some logging code to watch all your returns.

A: 

I've worked with at least one programmer who thought return was some special sort of function call, and was suprised when he saw that my code complied without the parens.

AShelly
And in the old days, in some contexts, it was implemented by a rather peculiar function, cret, at the assembler level. Now, that's a sweeping statement; I should find a reference. It was in Comer's XINU book - you can find that via a Google search at Purdue University.
Jonathan Leffler
+1  A: 

When returning -1 as in your excample, I think it's more readable with the parenthesis because the minus is more visible:

return 1

or

return -1

or

return (-1)
Stein G. Strindhaug