Skip to main content
Logo image

Section 2.2 Functions

A MATLAB function is a .m file that stores a sequence of MATLAB commands. Like a script, MATLAB executes those commands from top to bottom. The key difference is that a function is designed to accept inputs (known information) and return outputs (desired information).
You have already used many built-in MATLAB functions (such as sqrt, abs, and floor). The functions you write in this course work the same way: MATLAB only cares about the inputs you provide and the outputs the function returns.
Table 15. Scripts versus Functions
Feature Script Function
Goal Run a sequence of commands to complete a task Create a reusable tool (inputs → outputs)
Running it Press the “Run” button or type the script name in the command window Call it with inputs in the command window or another script or function
Workspace Shares the base workspace Uses its own workspace
Reusability Often specific to one situation Designed to be used many times