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

3 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Desktop 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by