LoginSignup
13
5

More than 5 years have passed since last update.

return ( x ); の由来

Last updated at Posted at 2017-11-30

注意: 2017年現在(というより規格化された1989年以降では)、return文の戻り値を括弧でくくる必要はありません。素直に return x; と書きましょう。


return文で戻り値を括弧()でくくる古風なコーデイングスタイルは、太古の C言語仕様に由来すると考えられます。

int foo()
{
  int x;
  /* 処理いろいろ... */
  return ( x );
}

C言語が"ANSI C"として規格化(1989年)されるより前の、書籍"K&R C 1st Ed."が刊行(1978年)されるより前の、UNIX v6時代にDennis Ritchie氏により書かれた"C Reference Manual"当時(1975年)の構文仕様では、return ( 式 ) ; のようにreturn文の戻り値を括弧でくくることが 必須でした

"C Reference Manual" §9.10 を引用します:

9.10 Return statement
A function returns to its caller by means of the return statement, which has one of the forms

return ;
return ( expression ) ;

In the first case no value is returned. In the second case, the value of the expression is returned to the caller of the function. If required, the expression is converted, as if by assignment, to the type of the function in which it appears. Flowing off the end of a function is equivalent to a return with no returned value.

おまけ:https://twitter.com/yohhoy/status/935850962614235137

13
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
5