## Custom Functions

We can build our custom functions to use for if we have to perform repeated task or for instance write the object function to optimize (ex. likelihood of a model).

Build a function is very easy, the first line always begin with this declare statement,

[output1, ouput2, …] = myfunctionname(input1,input2,…)

Let’s start with an example, we create a function to compute the square of a binomial.

function c = binomialsquare(a,b) c = a^2 + 2*a*b + b^2;

The difference from standard MATLAB script m-files are,

- Functions operate on a copy of the original data, it would replace if we modify in the function and we recall it in the output.
- What happens in the function stays in the function – any variable created when the function is running are lost when the function completes if they are not called in the output

There are a number of advanced function specific variables available to convey environmental parameters:

- how many input variables were provided to the function (nargin) or requested (nargout),
- optional input/output arguments returned by the function (varargin and varargout),
- allow for early termination of the function (return).

For example, let’s build a function to compute the square of binomial or a trinomial according the input,

function [output] = getsquared(a,b,c) % This function computes a binomial square if you insert two inputs a and b, % while it computes a trinomial square if you insert a, b, c. if nargin < 3 output = a^2 + 2*a*b + b^2; else output = a^2+ b^2 +c^2 +2*a*b + 2*b*c + 2*a*c; end

Now let’s type

help getsquared

Whenever help function is entered in the command window, MATLAB will display the first continuous block of comments in the command window (if they are present).

## Debugging

Since the data modified in the function is not available when the function is run, debugging can be done in three strategies,

- Write the “function” as a script and then convert it to a proper function,
- leave off “;” as needed to write out the value of variables to the command window. Alternatively, use disp.
- Use
*keyboard*and return to interrupt the function in order to inspect the values. - Adding keyboard to a function instructs MATLAB to interrupt the function and return control to the keyboard. When in this situation, the usual >> prompt changes to a K>>.
- When in keyboard mode, variables inside the function are treated as if they were script variables. Once finished inspecting the variables, enter return to continue the execution of the function.

function [output] = getsquared(a,b,c) % This function computes a binomial square if you insert two inputs a and b, % while it computes a trinomial square if you insert a, b, c. keyboard if nargin < 3 output = a^2 + 2*a*b + b^2; else output = a^2+ b^2 +c^2 +2*a*b + 2*b*c + 2*a*c; end keyboard

When *return* is entered, a second keyboard session open and so on.

function C = variousums(a,b,c) keyboard a = a + 1; keyboard b = a * b; keyboard c = c * b^2; keyboard