2.7. MATLAB Functions¶
Reading Assignment
Please read chapter 5 of Physical Modeling in MATLAB, by Allen B. Downey [DOWNEY11].
MATLAB Scripts are nice because they let us edit our code until we get it right and save the code in file for future use. But we really need something more. We have used many functions that are built into MATLAB, now we need to write our own functions.
 Functions have a private workspace, so there is less concern of name conflicts and the number of variables in the global workspace is kept in check.
 A function is a neat and tidy way to invoke code that is called multiple times. Functions allows common code to be called with different parameters or data, which is easier than replicating code.
 Knowing when to divide a program into separate functions versus keeping
the code together is some what an art form which is best perfected by
practice. Two questions will help with the design decisions.
 Does the code need to be executed more than once, especially with different values?
 Does the code provide a specific task? If so, putting it in a function may help to draw attention to the task and thus improve the clarity of the program.
 Functions may be placed either inside a script file or in their own file. Functions inside a script are good for functions that would only be used in that script. Place a function in its own file to make it generally available for other scripts and functions that you might develop.
2.7.1. Syntax of a Function¶
In MATLAB, the returned value(s) from a function are specified by their local variable name on the function definition line. The value of those variables at the end of the function, is their returned values. If there is more than one return value, they are listed like a vector.
The word function
is a keyword.

Function with one return values and two input arguments
function outPut = my1stFunction(input1, input2) % Help documentation code end

Function with two return values and two input arguments
function [outPut1, outPut2] = my2ndFunction(input1, input2) % Help documentation code end
Note
Create help text by inserting comments at the beginning of your program. If your program includes a function, position the help text immediately below the function definition line (the line with the function keyword). https://www.mathworks.com/help/matlab/matlab_prog/addhelpforyourprogram.html
2.7.2. Calling a Function¶
[out1, out2] = my2ndFunction(in1, in2);
The values of the input arguments and outputs are copied between where the function is called from and the function. Thus, the variable names between the two are completely independent. They may be the same name with no conflict of name space, or they may be completely different names. It does not matter.
The only important thing about the values is the order in the argument list and returned values.

Self Quiz
(Select all that apply) Which of the following are valid ways to call a function called myFunction which takes three inputs and returns two outputs?
 [out1,out2] = myFunction(in1,in2,in3)
 out1 = myFunction(in1,in2,in3)
 myFunction(in1,in2,in3)
Answer All of the syntaxes shown are valid. The function call returns only outputs that are requested. If no outputs are requested, the function still runs but does not return anything.

Quick Practice
The MATLAB function
isequal
compares two arrays and returns true if they are identical and false otherwise. In some situations, two numbers which are slightly different could be treated as ‘close enough’ for the purposes of a particular application. For example, a variablex
with value 2.1 and another variable y with value 2.100000009 differ by a small value on the order of .Write a function which takes three scalar inputs,
x
,y
, and a tolerancetol
. It should return true ifx
andy
differ by a number less thantol
and false otherwise. Add the function to the bottom of a script that tests the function.
2.7.3. Function Handles¶
MATLAB has a short cut way of expressing simple functions and can expressed,
usually as a math equation, with no Control Constructs.
In the following example, f
is the name of the function handle. The
argument(s) passed to the function are inside the parenthesis after the and
sign @( )
.
%% An example of an anonymous function handle
f = @(x) exp(x) .* sin(x);
t = linspace(0, 3*pi);
plot(t, f(t));
Another import application of function handles is to pass a function to another function.
See also
Note
Now work on Practice with Vectors and Functions, which we will work on together in class. Then work on Element by element Arithmetic, which is our first real programming assignment.