- 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.
Which requirements should be met when creating and using a function?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Aleksandar Denchev
el 7 de Mzo. de 2023
Comentada: Steven Lord
el 7 de Mzo. de 2023
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?
0 comentarios
Respuesta aceptada
Rik
el 7 de Mzo. de 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:
3 comentarios
Rik
el 7 de Mzo. de 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
el 7 de Mzo. de 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?
Más respuestas (0)
Ver también
Categorías
Más información sobre Desktop en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!