15

Quite often I see code like this (C, C++ and sometimes Java):

return (value);

I don't see any benefit of these parentheses. So my question is, have the programmers assumed return to be some kind of function with the return value as argument or are there really cases where these parentheses make sense?

I understand that a similar question has already been asked here, but this is related to ANSI C only. I wonder if there are aspects specific to C++ or Java that have not been answered there.

Community
  • 1
  • 1
Frank Puffer
  • 8,135
  • 2
  • 20
  • 45
  • 1
    `return` is a statement and the _parentheses_ ("brackets" is used for the `[]`) are useless. Grammatically they are part of the expression, not the statement. Modern coding styles often discourage using them. – too honest for this site Feb 22 '16 at 17:55
  • @WillBriggs: Well, it does grammatically. That might be very well relevant for code analysis. – too honest for this site Feb 22 '16 at 17:56
  • Will parentheses like these suppress conversion warnings from the type of value to the return type of the function? I know some other warnings, like assignments in conditional statements, can be suppressed with extra parens. – Sqeaky Feb 22 '16 at 18:07
  • 3
    There is an interesting case in C++14 where the `return` parentheses change the meaning: http://stackoverflow.com/questions/4762662/are-parentheses-around-the-result-significant-in-a-return-statement – Christian Hackl Feb 22 '16 at 18:15
  • it's in our coding guidelines. I wish it wasn't. – Alnitak Feb 22 '16 at 22:09
  • 1
    Possible duplicate of [return (a) vs. return a](http://stackoverflow.com/questions/7943052/return-a-vs-return-a) – Raedwald Feb 26 '16 at 07:54
  • It is not useful to ask this question for 3 different languages in one question. – Jonas Stein Nov 30 '19 at 21:45
  • @JonasStein Why do you think so. These languages are not unrelated. They have a lot I common. – Frank Puffer Dec 01 '19 at 20:55
  • 1
    @FrankPuffer C, C++ and JAVA are very different languages. You have to ask this question for each language to get a proper answer. You see that Haris tried to fix this by writing 3 answers at once. But this is not perfect in the sx metric. – Jonas Stein Dec 01 '19 at 21:40
  • I encountered this prediction in the 1980s. When I asked why, I was told 'to make it look like a function call'. I answered 'but it isn't a function call'. End of discussion ... – user207421 Nov 11 '20 at 06:40

4 Answers4

25

With respect to C

Parentheses are put where there is an expression and one wants the return value to be that value of the expression. Even then parentheses are not needed. It is completely ok to write something like

return x + y;

Programmers do make it return (x + y); to make it more readable.

So, putting parentheses is a matter of opinion and practice.


With respect to C++

There is an arcane case where parentheses matters. Quoting this question

int var1 = 42;
decltype(auto) func1() { return var1; } // return type is int, same as decltype(var1)
decltype(auto) func1() { return(var1); } // return type is int&, same as decltype((var1))

You can see the returned values are different, and that is due to the parentheses. You can go through this answer to understand in detail.


With respect to java, parentheses does not make any difference.

Coding convention suggests to go with

return x + y;

to understand more, read this answer.


NOTE: I do not know much about java and C++. All of the content in my answer about java and C++ are taken from other answers. I did this to consolidate the different conventions for the three languages.
Community
  • 1
  • 1
Haris
  • 12,120
  • 6
  • 43
  • 70
  • Summary: "So, putting brackets is a matter of opinion and practice.". Great!! – Elbert Villarreal Feb 22 '16 at 17:54
  • 4
    "to make it more readable" - not sure, it can also be misleading because it looks like a function call. – Frank Puffer Feb 22 '16 at 17:56
  • 4
    @FrankPuffer, no it doesn't. – SergeyA Feb 22 '16 at 17:57
  • @SergeyA I agree with +FrankPuffer. – RastaJedi Feb 22 '16 at 22:31
  • 1
    This Question is definitely a Troll. At first side The OP, looks like he needs to read about return statement and why and where parentheses are needed, but later he just not agree with Answers/Comments. I deleted my Answer after I read all his comments. – Michi Feb 23 '16 at 12:34
  • @Michi, May be. But that should not stop us from answering with the right information. Even if the OP disagrees. One can always try convincing the OP with more information. – Haris Feb 23 '16 at 12:48
  • 1
    @Michi: Just noticed this comment, after a long time. When posting this question I was pretty much aware that these parentheses are not needed aside from a few very esoteric cases. Still I wanted to understand better why programmers are using them, be it for technical or other types of reasons. Isn't this perfectly legitimate? For example I wanted to know how to react if I encounter this construct in a code review. The ansswers and comments here provided some helpful information to me. – Frank Puffer Apr 21 '19 at 07:41
5

Technically, there's no reason to parenthesize the expression given to return. It's a matter of taste really.

That said, I'll typically parenthesize a return expression if it uses boolean operators. Since such operators are typically seen as part of a while or if, it makes it more clear that you want to do something with that value besides jumping in my opinion.

int out_of_range(int x)
{
    return ((x < 1) || (x > 10));
}
dbush
  • 205,898
  • 23
  • 218
  • 273
4

Mostly legacy. K&R C Programming Language (1978) shows returns using parenthesis. It's possible that some old C compilers required this, but I'm not sure.

rcgldr
  • 27,407
  • 3
  • 36
  • 61
0

This is not mandatory first of all and you will see its importance in returning expressions & not values. But if you don't use parenthesis, your code may not be that easily readable(particularly in case of complex expressions) and may create confusion. Also,it may be noted that IT IS NOT A FUNCTION(as parenthesis is not compulsory).You may note the case of return 0; at the end of body of main(). Here it is noticeable that no parenthesis are used with 0 which proves that it's not a function. So, parenthesis are used to make the code more readable and understandable for the compiler.