# A Brief Introduction to Creating Functions in MATLAB

Hey wow, look at this!  I’ve finally rallied myself to write a blog article about something that is not digital audio!  Don’t get too excited though, this is still going to be a MATLAB article and, although I am not going to be getting too deep into any DSP, the fundamental techniques underlined in this article can be applied to a wide range of problems.

Now, let me go on record here and say I am not much of a computer programmer.  Thus, if you are looking for a guide to functional programming in general, this is not the place for you!  However, if you are perhaps an engineering student who’s learned MATLAB for school and are maybe interested in learning what this language is capable of, this is a good place to start.  Alternatively, if you are familiar with functional languages (*cough cough* Python), then this article may help you to start transposing your knowledge to a new language.

So What are Functions?

I am sure that, depending on who you ask, there are a lot of definitions for what a function actually is.  Functions in MATLAB more or less follow the standard signals-and-systems model of a system; this is to say they have a set of inputs and a corresponding set of outputs.  There we go, article finished, we did it!

Joking aside, there is not much more to be said about how functions are used in MATLAB; they are excellently simple.  Functions in MATLAB do provide great flexibility though because they can have as many inputs and outputs as you choose (and the number of inputs does not have to be the same as the number of outputs) and the relationship between the inputs and outputs can be whatever you want it to be.  Thus, while you can make a function that is a single-input-single-output linear-time-invariant system, you can also make literally anything else.

How to Create and Use Functions

Before you can think about functions, you’ll need a MATLAB script in which to call your function(s).  If you are familiar with an object oriented language (*cough cough* Java), the script is similar to your main method.  Below, I have included a simple script where we create two numbers and send them to a function called noahFactorial.

Simple Script Example

It doesn’t really matter what noahFactorial does, the only thing that matters here is that the function has two inputs (here X and Y) and one output (Z).

Our actual call to the noahFactorial function happens on line 4.  On the same line, we also assign the output of noahFactorial to the variable Z.  Line 6 has a print statement that will print the inputs and outputs to the console along with some text.

Now looking at noahFactorial, we can see how we define and write a function.  We start by writing ‘function’ and then defining the function output.  Here, the output is just a single variable, but if we were to change ‘output’ to ‘[output1, output2]’, our function would return a 2×1 array containing two output values.

Simple Function Example

Some of you more seasoned programmers might notice that ‘output’ is not given a datatype.  This will undoubtedly make some of you feel uncomfortable but I promise it’s okay; MATLAB is pretty good at knowing what datatype something should be.  One benefit of this more laissez-faire syntax is that ‘output’ itself doesn’t even have to be a single variable.  If you can keep track of it, you can make ‘output’ a 2×1 array and treat the two values like two separate outputs.

Once we write our output, we put an equals sign down (as you might expect), write the name of our function, and put (in parentheses) the input(s) to our function.  Once again, the typing on the inputs is pretty soft so those too can be arrays or single values.

In all, a function declaration should look like:

function output = functionName(input)

or…

function [output1, output2, …, outputN] = functionName(input1, input2, …,inputM)

And just to reiterate, N and M here do not have to be the same.

Once inside our function, we can do whatever MATLAB is capable of.  Unlike Java, return statements are not used to send anything to the output, rather they are used to stop the function in its tracks.  Usually, I will assign an output for error messages; if something goes wrong, I will assign a value to the error output and follow that with ‘return’.  Doing this sends back the error message and stops the function at the return statement.

So, if we don’t use return statements, then how do we send values to the output?  We make sure that in our function, we have variables with the same name as the outputs.  We assign those variable values in the function.  On the last line of the function when the function ends, whatever the values are in the output variables, those values are sent to the output.

For example, if we define an output called X and somewhere in our function we write ‘X=5;’ and we don’t change the value of X before the function ends, the output X will have the value: 5.  If we do the same thing but make another line of code later in the function that says ‘X=6;’, then the value of X returned will be: 6.  Nice and easy.

…And it’s that simple.  The thing I really love about functions is that they do not have to be associated with a script or with an object, you can just whip one up and use it.  Furthermore, if you find you need to perform some mathematical operation often, write one function and use it with as many different scripts as you want!  This insane flexibility allows for some insane problem-solving capability.

Once you get the hang of this, you can do all sorts of things.  Usually, when I write a program in MATLAB, I have my main script (sometimes a .fig file if I’m writing a GUI) in one folder, maybe with some assorted text and .csv files, and a whole other folder full of functions for all sorts of different things.  The ability to create functions and some good programming methodology can allow even the most novice of computer programmers to create incredibly useful programs in MATLAB.

NOTE: For this article, I used Sublime Text to write-out the examples.  If you have never used MATLAB before and you turn it on for the first time and it looks completely different, don’t be alarmed!  MATLAB comes pre-packaged with its own editor which is quite good, but you can also write MATLAB code in another editor, save it as a .m file, and then open it in the MATLAB editor or run it though the MATLAB kernel later.