Documentation Center

  • Trials
  • Product Updates

Create Functions in Files

This example shows how to create a function in a program file.

Write a Function

Open a file in a text editor. Within the file, declare the function and add program statements:

function f = fact(n)
f = prod(1:n);

Function fact accepts a single input argument n, and returns the factorial of n in output argument f.

The definition statement is the first executable line of any function. Function definitions are not valid at the command line or within a script. Each function definition includes these elements.

function keyword (required)

Use lowercase characters for the keyword.

Output arguments (optional)

If your function returns more than one output, enclose the output names in square brackets, such as

function [one,two,three] = myfunction(x)

If there is no output, either omit it,

function myfunction(x)

or 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 file and the first function within the file. MATLAB® associates your program with the file name, not the function name.

Input arguments (optional)

If your function accepts any inputs, enclose their names in parentheses after the function name. Separate inputs with commas, such as

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 allows you to call your function without specifying optional arguments.

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.

Functions end with either an end statement, the end of the file, or the definition line for another function, whichever comes first. The end statement is required only when a function in the file contains a nested function (a function completely contained within its parent).

Program files can contain multiple functions. The first function is the main function, and is the function that MATLAB associates with the file name. Subsequent functions that are not nested are called local functions. They are only available to other functions within the same file.

Save the File

Save the file (in this example, fact.m), either in the current folder or in a folder on the MATLAB search path. MATLAB looks for programs in these specific locations.

Call the Function

From the command line, call the new fact function to calculate 5!, using the same syntax rules that apply to calling functions installed with MATLAB:

x = 5;
y = fact(x);

The variables that you pass to the function do not need to have the same names as the arguments in the function definition line.

See Also

| | | |

Related Examples

More About

Was this topic helpful?