Main Content

Function Creation

Create functions, including anonymous, local, and nested functions

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.

MATLAB Language Syntax

functionDeclare function name, inputs, and outputs


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

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.