Function Creation
Functions contain one or more sequential commands and can accept inputs
and return outputs. When you have multiple lines of code, use the function
keyword to define a
function within a file. For instance, this function adds tax to a
price.
function total = addtax(price,taxrate) tax = price.*taxrate; total = price + tax; end
Alternatively, if you want to define a one-line function to pass to another function, you can create an anonymous function.
MATLAB Language Syntax
function | Declare function name, inputs, and outputs |
Topics
- Create Functions in Files
Store multiple commands in a program file that can accept inputs and return output.
- Types of Functions
There are several types of functions available with MATLAB®, including local functions, nested functions, private functions, and anonymous functions.
- Function Precedence Order
To determine which function to call when multiple functions in the current scope have the same name, MATLAB uses function precedence order.
- Add Help for Your Program
Add help text to your program that displays in the Command Window when you use the
help
function. - 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.