Should I indicate no input arguments to a function?
13 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Zoltán Csáti
el 23 de Sept. de 2017
Comentada: Walter Roberson
el 23 de Sept. de 2017
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 comentario
Stephen23
el 23 de Sept. de 2017
Editada: Stephen23
el 23 de Sept. de 2017
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?).
Respuesta aceptada
Walter Roberson
el 23 de Sept. de 2017
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 comentarios
Walter Roberson
el 23 de Sept. de 2017
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!