Introduction To Functions in Matlab: Syntax
Introduction To Functions in Matlab: Syntax
Functions operate on variables within their own workspace, which is also called the local
workspace, separate from the workspace you access at the MATLAB command prompt which
is called the base workspace.
Functions can accept more than one input arguments and may return more than one output
arguments.
Syntax
function [y1,...,yN] = myfun(x1,...,xM)
Description
function [y1,...,yN] = myfun(x1,...,xM) declares a function named myfun that accepts
inputs x1,...,xM and returns outputs y1,...,yN. This declaration statement must be the first
executable line of the function. Valid function names begin with an alphabetic character, and can
contain letters, numbers, or underscores.
You can save your function:
In a function file which contains only function
definitions. The name of the file should match the name
of the first function in the file.
In a script file which contains commands and function
definitions. Functions must be at the end of the file.
Script files cannot have the same name as a function in
the file. Functions are supported in scripts in R2016b or
later.
Files can include multiple local functions or nested functions. For readability, use
the end keyword to indicate the end of each function in a file. The end keyword is required
when:
Any function in the file contains a nested function.
The function is a local function within a function file,
and any local function in the file uses the end keyword.
The function is a local function within a script file.
1
Signals and Systems Lab DEE, FURC
Examples
Function with One Output
Define a function in a file named average.m that accepts an input vector, calculates the average
of the values, and returns a single result.
function y = average(x)
if ~isvector(x)
error('Input must be a vector')
end
y = sum(x)/length(x);
end
Call the function from the command line.
z = 1:99;
average(z)
ans =
50
Function in a Script File
function y = myIntegrand(x)
y = sin(x).^3;
end
y=
0.6495
a=
1.3333
2
Signals and Systems Lab DEE, FURC
function m = avg(x,n)
m = sum(x)/n;
end
Function avg is a local function. Local functions are only available to other functions within the
same file.
Call function stat2 from the command line.
values = [12.7, 45.4, 98.9, 26.6, 53.1];
[ave,stdev] = stat2(values)
ave =
47.3400
stdev =
29.4124
More Examples
The following function named mymax should be written in a file named mymax.m. It takes five
numbers as argument and returns the maximum of the numbers.
3
Signals and Systems Lab DEE, FURC
Create a function file, named mymax.m and type the following code in it −
max = n1;
max = n2;
end
max = n3;
end
max = n4;
end
max = n5;
end
The first line of a function starts with the keyword function. It gives the name of the function
and order of arguments. In our example, the mymax function has five input arguments and one
output argument.
The comment lines that come right after the function statement provide the help text. These
lines are printed when you type −
help mymax
MATLAB will execute the above statement and return the following result −
4
Signals and Systems Lab DEE, FURC
Nested Functions
You can define functions within the body of another function. These are called nested functions.
A nested function contains any or all of the components of any other function.
Nested functions are defined within the scope of another function and they share access to the
containing function's workspace.
Let us rewrite the function quadratic, from previous example, however, this time the disc
function will be a nested function.
d = sqrt(b^2 - 4*a*c);
disc;
x1 = (-b + d) / (2*a);
5
Signals and Systems Lab DEE, FURC
x2 = (-b - d) / (2*a);
quadratic2(2,4,-4)
MATLAB will execute the above statement and return the following result −
ans = 0.73205
Private Functions
A private function is a primary function that is visible only to a limited group of other functions.
If you do not want to expose the implementation of a function(s), you can create them as private
functions.
Example
Let us rewrite the quadratic function. This time, however, the disc function calculating the
discriminant, will be a private function.
Create a subfolder named private in working directory. Store the following function
file disc.m in it −
Create a function quadratic3.m in your working directory and type the following code in it −
% a quadratic equation.
6
Signals and Systems Lab DEE, FURC
%constant term
d = disc(a,b,c);
x1 = (-b + d) / (2*a);
x2 = (-b - d) / (2*a);
quadratic3(2,4,-4)
MATLAB will execute the above statement and return the following result −
ans = 0.73205
Global Variables
Global variables can be shared by more than one function. For this, you need to declare the
variable as global in all the functions.
If you want to access that variable from the base workspace, then declare the variable at the
command line.
The global declaration must occur before the variable is actually used in a function. It is a good
practice to use capital letters for the names of global variables to distinguish them from other
variables.
Example
Let us create a function file named average.m and type the following code in it −
global TOTAL
avg = sum(nums)/TOTAL;
end
7
Signals and Systems Lab DEE, FURC
global TOTAL;
TOTAL = 10;
n = [34, 45, 25, 45, 33, 19, 40, 34, 38, 42];
av = average(n)
When you run the file, it will display the following result −
av = 35.500