Brackets surrounding return values

Quite often in ANSI C code, I see parentheses surrounding a single return value.

Like this: -

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

Why use () around the return value in these cases? Any ideas? I see no reason for this.

+66
c syntax coding-style
02 Oct '08 at 11:52
source share
12 answers

There really is no reason ... it's just an old deal.

To save space, programmers often did the final math in the return line rather than on their own line, and in most cases the parens functions make it easy to see that this is the only statement that returns:

 return (x+i*2); 

instead

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

The bracket has become a habit and stuck.

+56
Oct 02 '08 at
source share

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

+47
Oct 02 '08 at 19:26
source share

My personal style is to use parentheses if a complex expression exists; eg.

 return (a + b); 

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

 return a; 

I can’t say why I do this; just something that I took a long time.

By the way, I think it looks like a function call, for example:

 return(a); // ugh 

incredibly ugly and just plain wrong.

+22
Oct 02 '08 at 12:24
source share

There are several reasons:

  • if / then / for / etc .. all the control keywords that should have parens. Therefore, it often seems natural that they always bring them back.

  • sizeof is the only other keyword that may or may not have them, except in some cases you should use parens. So it’s easier to get used to always use parsers. for sizeof, which implies logic: if you can, always do it.

  • case / goto are the only keywords in which you never use parens .... and people tend to think of them as special cases (and how they both stand out from other control keywords, especially).

+13
Oct 02 '08 at 14:35
source share

In the original C specification, parentheses were required around the return value. Although 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 add to the habit of not knowing the standards to meet the stylistic convention that requires them, or possibly for backward compatibility.

I have to add, for people who think of C ++: This question is about C, not C about C ++; they are two different languages ​​with different standards, capabilities, levels of complexity and different styles of use that appear - no matter what they have in common, it is reasonable to consider them as two completely different things. For a similar question that C ++ covers, see Are parentheses important around the result in the return statement? ,

+12
Oct 21 '17 at 20:32
source share

When returning -1, as in your example, I think this is more readable with a parenthesis, because the minus is more noticeable:

 return 1 

or

 return -1 

or

 return (-1) 
+8
Oct 08 '08 at 13:31
source share

Perhaps this is a custom - in the end, the people who brought us Unix and C came from the Multics project. Cartoons were written in PL / I, and in PL / I, brackets are required.

+3
Apr 25 '14 at 16:29
source share

As is often the case when using parentheses, I think that it is only for readability (for example, Ruby supports method calls without parentheses that include arguments, but recent books and articles advise otherwise).

+2
Oct 02 '08 at 11:55
source share

I worked with at least one programmer who thought that returning was some kind of special function call, and was surprised when I saw that my code executed without partners.

+2
Oct 02 '08 at 20:26
source share

Perhaps this is due to the fact that in brackets it looks more like a function call, i.e. more like the rest of the code?

Or is it just what everyone does, just because everyone does it :-)

0
02 Oct '08 at
source share

In parentheses, the return statement tells the compiler that you intend to return this value to the stack instead of memory.

In the old days, this was strictly observed (usually), but today most compilers take this as a hint.

This is what I often do, since an error can ruin anything returned via a memory reference, but usually does not affect the return variable on the stack.

Using a stack for transient variables also reduces memory usage and usually makes the call / return function faster, because that's what the stack is for, temporary data / variables.

-four
Mar 26 '16 at 6:33
source share

The use of parentheses in the return statement shows a lack of understanding of C/C++ syntax. It is so simple. But this is not so bad as putting everything in braces:

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

So many programmers do this. If any of you are reading this, you might want to explain.

-6
Jan 21 '11 at 19:09
source share



All Articles