89

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.

Jonas Stein
  • 6,826
  • 7
  • 40
  • 72
Tooony
  • 3,741
  • 2
  • 17
  • 13
  • 1
    It looks like a function call that way ;-). – Toon Krijthe Oct 02 '08 at 11:55
  • 3
    But, return is not a function call, unless you're a Schemer. :-P – C. K. Young Oct 02 '08 at 11:59
  • While post hoc ergonomic justifications are often offered for the habit, it is worth looking at modern languages that have return statements and asking whether the same convention exists. AFAIK the answer is a resounding NO. User10392's answer is interesting in that it gives a C-specific benefit deriving from the habit. – AmigoNico Jun 19 '13 at 02:33
  • Possible duplicate of [Are parentheses around the result significant in a return statement?](http://stackoverflow.com/questions/4762662/are-parentheses-around-the-result-significant-in-a-return-statement) – phuclv May 17 '17 at 12:41

11 Answers11

62

There isn't really a reason... it's just an old convention. Editor's note: There are historical reasons.

To save space, programmers would often do the final math in the return line instead of on its own line and the parens are mostly there to make it easier to see that it is a single statement that is returned, like this:

return (x+i*2);

instead of

int y = x+i*2;
return y;

The parenthesis became a habit and it stuck.

tom
  • 21,844
  • 6
  • 43
  • 36
Adam Haile
  • 30,705
  • 58
  • 191
  • 286
  • 3
    "the parens ensure are mostly there to make it easier to see that it is a single statement that is returned" - what is this even supposed to mean? apart from the grammar, you can't return statements, and it uses more space than without parens. – Remember Monica Oct 17 '14 at 11:31
  • 3
    Sorry to revive such an old answer, but there actually *is* a reason. In the very early days of C, before it was standardized, the parenthesis were required by the syntax. Refer to http://cm.bell-labs.com/cm/cs/who/dmr/cman.ps. – Segmented Mar 27 '15 at 23:22
  • 3
    @MarcLehmann I have no idea what it's supposed to mean or how it got so highly voted & accepted. Adding parentheses just looks ugly to me (I might even get rid of them for `if` et al, if I were king). As for `make it easier to see that it is a single statement that is returned`, firstly, that's an _expression_, not a "statement" - & what - do people think `return` is a greedy operator and that `return x+i*2` might return `x ` and discard the rest? If so, wow. Or was this answer really thinking about long expressions that might span multiple lines? Either way, it's totally missed its own point. – underscore_d Jul 30 '16 at 23:36
57

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.

user10392
  • 1,384
  • 8
  • 12
  • 1
    Interesting observation. In my experimentation I noticed that a macro defined for **return** appears to allow macro arguments without parens, but a macro defined for another name (say **square**) doesn't. I think I've gazed into the abyss this time... – Alain O'Dea Aug 29 '15 at 15:20
  • 1
    ... better hope nobody calls a function or uses an increment operator or has any other side effects in their return statements, then. – Carl Norum Sep 29 '16 at 18:18
  • @CarlNorum would you please give a simple example? – polynomial_donut Sep 18 '18 at 21:28
  • 1
    @polynomial_donut - if you had an expression in your returns statement, normal macro-related problems like multiple evaluation might take place. – Carl Norum Sep 19 '18 at 17:45
34

In the original C specification, parentheses were required around the return value. While modern C compilers and the ANSI C standard do not require them, the presence of parentheses does not affect the return value, and programmers sometimes still include them out of habit, unfamiliarity with the standards, for consistency with a stylistic convention that requires them, or possibly for backward compatibility.

I should add, for people that are thinking about C++: This question is about C and C is not C++; these are two different languages with different standards, capabilities, levels of difficulty, and different styles of usage that emerge -- whatever they have in common, it is wise to treat them as two totally separate things. For a similar question that covers C++, see Are parentheses around the result significant in a return statement?.

rakslice
  • 8,742
  • 4
  • 53
  • 57
  • 3
    IMHO, this is the only reasonable/convincing answer (as it explains where a strange habit may originate from). I came here from [SO: Are parentheses around the result significant in a return statement?](https://stackoverflow.com/a/25615981/7478597) just having learned another significant difference between C and C++... – Scheff's Cat Apr 24 '19 at 10:11
23

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
  • 81,409
  • 55
  • 245
  • 302
15

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
  • 2,825
  • 18
  • 16
7

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
  • 5,077
  • 2
  • 28
  • 41
6

Perhaps it's custom--after all, the folks who brought us Unix and C came from the Multics project. Multics was written in PL/I, and in PL/I the parentheses are mandatory.

James Jones
  • 131
  • 1
  • 2
3

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
  • 34,686
  • 15
  • 91
  • 152
  • 1
    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 Oct 03 '08 at 02:18
2

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

RastaJedi
  • 641
  • 1
  • 6
  • 18
Manrico Corazzi
  • 11,299
  • 10
  • 48
  • 62
-4

The Parenthesis in a return statement indicate to the compiler that you intend for this value to be returned on the stack instead of in memory.

In the old days this was rigorously enforced(typically), but today most compilers only take it as a hint.

This is something I do frequently, since an error could corrupt anything being returned via a memory reference, but typically wont effect a variable being returned on the stack.

Using the stack for transient variables also cuts down on memory usage and typically makes the function call/return quicker because that's what the stack is designed for, transient data/variables.

  • 1
    If indeed there is a compiler that does this, it would be great to show an example of the assembly generated by the compiler for each case – rakslice Jun 29 '18 at 19:33
-6

Using parentheses in a return statement shows a deficient grasp of C/C++ syntax. It's as simple as that. But it's not as bad as putting everything in curly braces:

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

So many programmers do this. If one of you reads this, perhaps you might like to explain.

TonyK
  • 16,761
  • 4
  • 37
  • 72
  • 16
    Usually, braces are used to reduce the probability of introducing mistakes in future edits of the code. The first answer to this question gives an example: http://programmers.stackexchange.com/questions/16528/single-statement-if-block-braces-or-no – Mike Pelley Aug 25 '11 at 19:08
  • 4
    Also, always using braces make the code much more automatically merge friendly. Otherwise you are much more likely to get conflicts that must be handled manually. – hlovdal Dec 14 '12 at 06:27
  • 6
    There are innumerable cases of bugs introduced due to lacking curly braces. I agree they are ugly for one line but when you replace a function call with a macro - as it sometimes happens due to special constraints - or when a program is refactored to replace a one line expression with a longer set of expressions the bugs might not be immediately apparent and happen only in those special cases that are not automatically testable. The curly braces are also a hardcoded requirement in some companies (I had that at a military contracting company) even for 1 liners. – Coyote May 10 '13 at 13:48
  • 2
    MISRA explicitly forbids removing those "extra" braces. – Toby Jul 08 '15 at 08:38