Create Functions in Files
Both scripts and functions allow you to reuse sequences of commands by storing them in program
files. Scripts are the simplest type of program, since they store commands exactly as you would type
them at the command line. Functions provide more flexibility, primarily because you can pass input
values and return output values. For example, this function named fact computes the factorial of a
number (n) and returns the result (f).
function f = fact(n)
f = prod(1:n);
end
This type of function must be defined within a file, not at the command line. Often, you store a
function in its own file. In that case, the best practice is to use the same name for the function and
the file (in this example, fact.m), since MATLAB associates the program with the file name. Save the
file either in the current folder or in a folder on the MATLAB search path.
You can call the function from the command line, using the same syntax rules that apply to functions
installed with MATLAB. For instances, calculate the factorial of 5.
x = 5;
y = fact(5)
y =
120
Starting in R2016b, another option for storing functions is to include them at the end of a script file.
For instance, create a file named mystats.m with a few commands and two functions, fact and
perm. The script calculates the permutation of (3,2).
x = 3;
y = 2;
z = perm(x,y)
function p = perm(n,r)
p = fact(n)/fact(n-r);
end
function f = fact(n)
f = prod(1:n);
end
Call the script from the command line.
mystats
z =
6
Syntax for Function Definition
The first line of every function is the definition statement, which includes the following elements.
20 Function Basics
20-2
Both scripts and functions allow you to reuse sequences of commands by storing them in program
files. Scripts are the simplest type of program, since they store commands exactly as you would type
them at the command line. Functions provide more flexibility, primarily because you can pass input
values and return output values. For example, this function named fact computes the factorial of a
number (n) and returns the result (f).
function f = fact(n)
f = prod(1:n);
end
This type of function must be defined within a file, not at the command line. Often, you store a
function in its own file. In that case, the best practice is to use the same name for the function and
the file (in this example, fact.m), since MATLAB associates the program with the file name. Save the
file either in the current folder or in a folder on the MATLAB search path.
You can call the function from the command line, using the same syntax rules that apply to functions
installed with MATLAB. For instances, calculate the factorial of 5.
x = 5;
y = fact(5)
y =
120
Starting in R2016b, another option for storing functions is to include them at the end of a script file.
For instance, create a file named mystats.m with a few commands and two functions, fact and
perm. The script calculates the permutation of (3,2).
x = 3;
y = 2;
z = perm(x,y)
function p = perm(n,r)
p = fact(n)/fact(n-r);
end
function f = fact(n)
f = prod(1:n);
end
Call the script from the command line.
mystats
z =
6
Syntax for Function Definition
The first line of every function is the definition statement, which includes the following elements.
20 Function Basics
20-2
