Which requirements should be met when creating and using a function?

1 回表示 (過去 30 日間)
Aleksandar Denchev
Aleksandar Denchev 2023 年 3 月 7 日
コメント済み: Steven Lord 2023 年 3 月 7 日
1.The input arguments of the functions should be in brackets () and the output arguments should be in straight brackets
[], both separeted by commas.
2.A function file starts with the code function.
Are these both correct or incorrect, or just one of them is correct?

採用された回答

Rik
Rik 2023 年 3 月 7 日
For my minify function I needed to construct a full list of all legal syntaxes, so I just tried them all.
function[a,b]=f1(c,d)
function e=f2
These two syntaxes are the most compact ones. Extra spaces are allowed at every point (except inside the keyword function, of course).
As far as the rest of the file if you want the function to be callable:
  • Matlab will call the first function in the file. You should have the first function in the file match the file name. If you don't, no function is allowed to have the same name as the file.
  • No code may exist before the first function keyword.
  • Either all functions must be closed with an end keyword, or none of them.
  3 件のコメント
Rik
Rik 2023 年 3 月 7 日
The second is not strictly correct, as there are allowed to be comments and whitespace prior to the first function keyword, but other thant that, yes.
Steven Lord
Steven Lord 2023 年 3 月 7 日
See the "Syntax for Function Definition" section on this documentation page for a description of what a function definition statement must and can include.
If I were to be pedantic (me? never! :) your first statement is a little ambiguous when it says "functions". Does that include anonymous functions or does it only include the first two categories on this documentation page?

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by