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. 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

functionDeclare function name, inputs, and outputs

Topics

Go to top of page