Help us understand the problem. What is going on with this article?

return ( x ); の由来

More than 3 years have passed since last update.

注意: 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.


Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away