A function is a programming device that lets you compartmentalize and tightly control the way in which computations are done in MATLAB. For example, you probably are aware of the built-in functions in MATLAB such as sqrt, sin, exp, log and so forth. These functions compartmentalize the computation of specific mathematical functions, relieving you of having to explicitly carry out the computations every time you need to have access to those functions.
Functions permit you to view the flow of logic through a computation at a higher level of abstraction. For example, consider taking the square-root of a number. There are many approaches to this computational problem but from the point of view of getting and using the square-root of a number, the method of computation is not really of primary concern. Rather, knowing that the command
w = sqrt(3)
will compute the value of the square-root of 3 and then assign the result to the variable called w lets you focus on the assignment process and not the details of getting the value used in the assignment.
This higher level of abstraction is illustrated in the
figure below which shows the process of using a function (with
the square-root function as an example):
An input-output view of a MATLAB function
Note that what goes on inside the function is not known explcitly. The function is a black-box, from this point of view. However, what is important is that the function will take a number as input and produce, as a result of some computational procedure, an output which is the square root of that number. Other ways of saying the same thing are
The higher level ideas of transformation and operation lend themselves
more easily to the development of programs (scripts, other functions) that
implement problem-solving algorithms.
The primary reason for using a function is to compartmentalize a particular computation. The computation of mathematical functions (e.g., Bessel functions, trigonometric functions, exponential functions or statistical distributions) is one important reason to "compartmentalize". When was the last time you worried about what the SIN button on your calculator actually did? You were probably more interested in the result of the function rather than how the function was executed.
You can generalize this idea to the compuation of things of more direct use to your professional life. For example, in chemical engineering, you might want to "compartmentalize" activities like
While each of these tasks involves a good deal of detail, they are each "basic" to the chemical engineering profession in the same way that mathematical functions are "basic" to mathematicians.
Beyond compartmentalization, functions play two imporant roles in MATLAB programing
All functions have the following elements:
All function Mfiles should start with a function definition line. The general form of this line is
function [out1,out2,... ,outN] = fname(in1,in2,...,inM)
The definition line sets out the "contract" for the function. The function will be called fname and is stored in the file fname.m. The function is to take input arguments (in1,in2,...,inM) and produce output arguments (out1,out2,... ,outN). The number of inputs (M) and outputs (N) can be zero or any positive integer.
Examples: Identify the name of the function, the number of inputs and the number of outputs from each of the following definition lines.
The key syntax elements of the definition line are
The keyword function must be the first word on
the definition line.
The keyword function differentiates a function Mfile from
a script Mfile and so it is
important to include it in an Mfile destined to be a function Mfile. There
should be a space between the word function and the rest of
the definition
line.
There is a permanent variable called nargout which provides
the number of output arguments used
in the call to the function.
Do not use the name of a built-in function as the name of a function
you create.
There is a permanent variable called nargin which may be used by the function to determine the number of input arguments that were provided in the call to the function.
The first unbroken set of comment lines that follow the function definition line are called the H1 help lines. These lines are displayed when a user issues the on-line help command for the function. For example, the following portion of a function provides an idea of how to structure the H1 help information.
function polyplot(x,y,n) %POLYPLOT Fit and plot a polynomial of degree n to a set of data (x,y) % % ployplot(x,y,n) fits an nth order polynomial to a set of x-y data and % plots the data and the fit on the same set of axes, using symbols for % the data and a solid line for the fit. % % If n is omitted, n = 1 is assumed. % This comment is not part of the H1 help section since a blank line % separates it from the prior comment lines
If you put these lines in the file polyplot.m, then typing
>> help polyplot
will generate the output
POLYPLOT Fit and plot a polynomial of degree n to a set of data (x,y) ployplot(x,y,n) fits an nth order polynomial to a set of x-y data and plots the data and the fit on the same set of axes, using symbols for the data and a solid line for the fit. If n is omitted, n = 1 is assumed.
Note that once a blank, un-commented line is encountered, no further information is displayed to the screen.
An effort should be made to write succinct, informative help lines for yourself and for others using the functions you write. For example, provide a summary of the syntax (input and output arguments) and notify the user of any default behavior.
Within these guidelines, much is possible in the realm of programming
with a function Mfile.