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

1 visualizzazione (ultimi 30 giorni)
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?

Risposta accettata

Rik
Rik il 7 Mar 2023
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 Commenti
Rik
Rik il 7 Mar 2023
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 il 7 Mar 2023
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?

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by