Chapter 8: Functions and Function Handles

What is a Function?

At this point, students have learned many commands within ME 160 and have the tools to complete operations through more and more complex codes. As codes become longer and more complicated it is necessary to find ways to reuse common sections which are used in multiple scripts efficiently. MATLAB has a file type called functions which enables the user to create their own functions in separate script files and refer to them in another script they are writing. This is handy when using the same equations several times in different codes and would like to save time and not repeat the same code over and over.

How to Create a Function in MATLAB

A function can be created in a similar method to normal scripts. By having the first line of a function contain the function command, the script will be saved as a function file and not as a .m MATLAB script. The following shows a very simple function that can compute the factorial of some number n by summing every integer between one and n. By having “function” being the first thing in the script,  MATLAB can automatically determine that this is not a full script, but instead just a function that may be called to a different script.

function f = fact(n)

    f = prod(1:n);


In this case, the function is called “fact”. When saving the newly created file, it must be called “fact” in order for the function to work properly. After saving this function as its own .m file, the user can call the function like any other MATLAB command in a script that is being written. Remember to save the function file with the same name as the function, so the .m file should appear as “fact.m” in this example. This is a very easy mistake to make that will prevent MATLAB from finding the correct function. Another word of warning, remember to rename the function file if you rename the function while editing the code during revisions! Calling the function in a script is shown in the following example. Within this script, a variable “x” is assigned a value. Then, MATLAB will assign the variable “y” a value after completing the operation within the “fact” function. Notice this operation takes the value assigned to “x” as input and enters that into the function for the variable “n“. This demonstrates that variable names do not need to be consistent between the function and the script, as the function will accept any variable entered in the correct location. This enables a function to be used several times for several different values throughout a script, demonstrating the simplifying benefits of functions.

x = 5;

y = fact(x);

y =


Function Example: Quadratic Equation

An example of an application for creating a function in a script is included below. This example contains a script that would like to use the quadratic equation. Instead of utilizing equations within the base script, a separate quadratic equation function is created and used for this application. The function, entitled “quadraticsolver” begins with a line defining that the script is a function that outputs a variable “x” when given inputs for variables “a”, “b”, and “c”. Within the main script “QuadraticFunction”, a user inputs values for variables “a”, “b”, and “c”. These variables are plugged into the function “quadraticsolver” in line 9. This completes the arithmetic included in the “quadraticsolver” function file and outputs the results to x. Notice how in this example the variables “a”, “b”, “c”, and “x” are used in both the function and main script. When using the function, different variables could have been used. For example “x = quadraticsolver(i,j,k)” will work the same as the code cares only about the order the function is fed information, not the actual variable names. This means that the naming conventions of a script do not need to be changed to match a function’s naming.


Icon for the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License

A Guide to MATLAB for ME 160 Copyright © 2022 by Austin Bray and Reza Montazami is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License, except where otherwise noted.