Introduction to Functions in MATLAB#

Here we will take a look at what a function is and the basic components that make up a function.

What is a function?#

A function is a block of code concatenated into a single line. Custom functions, which I will be teaching here, are especially helpful if you plan on executing lots of repeated code. mean() and heatmap() are both functions, for example. When you execute a function, you are “calling” it and executing the code embedded within it.

Let’s see how this works.

X = [1, 2, 3, 4, 5, 6, 7]
ans = mean(X)
sprintf('The mean of X is %g'., ans)
            Error: MATLAB Kernel for Jupyter was unable to find the notebook server from which it was spawned!

            Resolution: Please relaunch kernel from JupyterLab or Classic Jupyter Notebook.
            

In the above cell, you simply typed mean(...) and MATLAB was able to calculate the mean of your desired input. As stated earlier, there is code embedded within this function that is computed when mean() is called. But what is the code for mean()? In MATLAB, type open mean. This will open the mean.m file where the code to calculate the mean of a given input is located. Scroll through the file. It’s relatively large (\(\approx\) 270 lines of code)!

What is the structure of a function?#

In MATLAB, to build a function requires a few syntactical components, some of which differ from other programming languages. The basic formula is this:

function [output] = myFunction(input)

... % where your code will go

end

Let’s build our own function to calculate the sum of two numbers.

function ans = mySum(a, b)

ans = a + b
sprintf('%g + %g = %g', a, b, ans)

end
            Error: MATLAB Kernel for Jupyter was unable to find the notebook server from which it was spawned!

            Resolution: Please relaunch kernel from JupyterLab or Classic Jupyter Notebook.
            

Here, we have created a function, called mySum() that summates two numbers and outputs this answer. a and b are the two inputs that we wish to manipulate. ans is the calculated sum of our two inputs.

Futher details about building functions.#

There are a few important practices to be aware of when making functions.

  1. Typically, functions are created in seperate MATLAB files. You can create a function script by clicking New >> Function. This opens a template for your function which looks something like this:

function [outputArg1,outputArg2] = untitled4(inputArg1,inputArg2)
%UNTITLED4 Summary of this function goes here
%   Detailed explanation goes here
outputArg1 = inputArg1;
outputArg2 = inputArg2;
end
  1. If you are creating a function locally, meaning within your script and not in a separate file, then the function or functions must be created at the end of the script. It is alright if you call the functions earlier–this is just a rule in MATLAB. If you create your function and then attempt to call it after you have created it, you will receive this error:

Function definitions in a script must appear at the end of the file.
Move all statements after the "mySum" function definition to before the first local function
definition.
  1. Functions should contain a docstring or an explanation of the function’s implementation.

Let’s take a look at some of these key points.

% call function
mySum(2, 3)

function ans = mySum(a, b)
%MYSUM Summates two numbers together.
    % a (double or scalar) Can be positive or negative integer
    % b (double or scalar) Can be positive or negative integer
    
% computes sum
ans = a + b
    
% displays output
sprintf('%g + %g = %g', a, b, ans)

end
            Error: MATLAB Kernel for Jupyter was unable to find the notebook server from which it was spawned!

            Resolution: Please relaunch kernel from JupyterLab or Classic Jupyter Notebook.
            

Here, I have added some documentation to the function to make it more readable. Remember, because I am creating the function locally, I must call the function before I define the function.

And that’s it! Functions are a very important component to programming and learning how to build and leverage their use can significantly bolster one’s programming abilities and fluency.