Functions contain one or more sequential commands and can accept inputs and return outputs. To write a program with multiple lines of code, create a named function in a file.
Alternatively, if you want to define a one-line function to pass to
another function—for instance, a mathematical expression to pass to the
integral
function—you can create an anonymous function.
function | Declare function name, inputs, and outputs |
Store multiple commands in a program file that can accept inputs and return output.
There are several types of functions available with MATLAB®, including local functions, nested functions, private functions, and anonymous functions.
To determine which function to call when multiple functions in the current scope have the same name, MATLAB uses function precedence order.
Help text appears in the Command Window when you use the
help
function. Create help text for your program by
inserting comments at the beginning.
Indexing into Function Call Results
This topic describes how to dot index into temporary variables created by function calls.
Configure the Run Button for Functions
Run functions that require some initial setup from the Editor by configuring the Run button.