function keyword
(required)
Use lowercase characters for the keyword.
Output arguments (optional) If your function returns one output, you can specify the output name
after the function keyword.
function myOutput = myFunction(x)
If your function returns more than one output, enclose the output
names in square brackets.
function [one,two,three] = myFunction(x)
If there is no output, you can omit it.
function myFunction(x)
Or you can use empty square brackets.
function [] = myFunction(x)
Function name (required)
Valid function names follow the same rules as variable names. They
must start with a letter, and can contain letters, digits, or
underscores.
Note To avoid confusion, use the same name for both the function file
and the first function within the file. MATLAB associates your
program with the file name, not the function name. Script files cannot
have the same name as a function in the file.
Input arguments (optional)
If your function accepts any inputs, enclose their names in
parentheses after the function name. Separate inputs with commas.
function y = myFunction(one,two,three)
If there are no inputs, you can omit the parentheses.
Tip When you define a function with multiple input or output arguments, list any required arguments
first. This ordering allows you to call your function without specifying optional arguments.
Contents of Functions and Files
The body of a function can include valid MATLAB expressions, control flow statements, comments,
blank lines, and nested functions. Any variables that you create within a function are stored within a
workspace specific to that function, which is separate from the base workspace.
Program files can contain multiple functions. If the file contains only function definitions, the first
function is the main function, and is the function that MATLAB associates with the file name.
Functions that follow the main function or script code are called local functions. Local functions are
only available within the file.
Create Functions in Files
20-3
(required)
Use lowercase characters for the keyword.
Output arguments (optional) If your function returns one output, you can specify the output name
after the function keyword.
function myOutput = myFunction(x)
If your function returns more than one output, enclose the output
names in square brackets.
function [one,two,three] = myFunction(x)
If there is no output, you can omit it.
function myFunction(x)
Or you can use empty square brackets.
function [] = myFunction(x)
Function name (required)
Valid function names follow the same rules as variable names. They
must start with a letter, and can contain letters, digits, or
underscores.
Note To avoid confusion, use the same name for both the function file
and the first function within the file. MATLAB associates your
program with the file name, not the function name. Script files cannot
have the same name as a function in the file.
Input arguments (optional)
If your function accepts any inputs, enclose their names in
parentheses after the function name. Separate inputs with commas.
function y = myFunction(one,two,three)
If there are no inputs, you can omit the parentheses.
Tip When you define a function with multiple input or output arguments, list any required arguments
first. This ordering allows you to call your function without specifying optional arguments.
Contents of Functions and Files
The body of a function can include valid MATLAB expressions, control flow statements, comments,
blank lines, and nested functions. Any variables that you create within a function are stored within a
workspace specific to that function, which is separate from the base workspace.
Program files can contain multiple functions. If the file contains only function definitions, the first
function is the main function, and is the function that MATLAB associates with the file name.
Functions that follow the main function or script code are called local functions. Local functions are
only available within the file.
Create Functions in Files
20-3
