Should I indicate no input arguments to a function?

14 ビュー (過去 30 日間)
Zoltán Csáti
Zoltán Csáti 2017 年 9 月 23 日
コメント済み: Walter Roberson 2017 年 9 月 23 日
Suppose I have a function which requires no inputs. Should I indicate it with parentheses () or leave it naked? Both cases work, but is there a convention in the MATLAB community?
Example:
someFunction
% do sth
end
vs
someFunction()
% do sth
end
  1 件のコメント
Stephen23
Stephen23 2017 年 9 月 23 日
編集済み: Stephen23 2017 年 9 月 23 日
Use parentheses. It makes the intention clear (that you meant to define a function with no arguments), and allows you to easily check if a line-end has accidentally been altered (who has never deleted/pasted to the wrong location?).

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2017 年 9 月 23 日
If a function requires no inputs, I prefer to use () after calls to it. It is unambiguous, and saved remember whether the name is a function directly or a function handle (function handles require the () to call them)
  2 件のコメント
Zoltán Csáti
Zoltán Csáti 2017 年 9 月 23 日
"prefer to use () after calls to it". At the call site, I also put () to differentiate from a variable. If I understood well you also put for the definition (as in my example above), do you?
Walter Roberson
Walter Roberson 2017 年 9 月 23 日
If I am creating a function with no arguments then it is most likely because I am answering a question here, tossing together something of no lasting importance. In such cases I usually do not bother to put the () in the function definition. In my own code, I do not typically have functions with no parameters.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by