Functions in MATLAB
Learn how to define and call functions in MATLAB — multiple return values, default arguments, recursion, anonymous functions, and higher-order functions with Docker-ready GNU Octave examples
Functions let you package logic into reusable, named units. In MATLAB they are central to writing anything beyond a short script: every toolbox you use is built from functions, and even the built-in disp and fprintf you met in Hello World are just functions.
MATLAB’s function model has a few characteristics that set it apart from many languages. Functions can return multiple values natively — no tuples, structs, or out-parameters required. Argument counts are flexible and inspected at runtime with nargin and nargout, which is how MATLAB handles “default” arguments. And because MATLAB is array-first, functions are typically written to operate on whole matrices at once rather than scalar-by-scalar.
This tutorial covers defining and calling functions, returning one or many values, default arguments, recursion, and anonymous functions (function handles) — MATLAB’s lightweight, first-class functions that enable higher-order programming with tools like arrayfun.
A note on file structure: In modern MATLAB (R2016b+) and in GNU Octave, a script file may contain local functions, but they must appear after all the script code. The examples below follow that order so they run unchanged in both environments.
Defining and Calling Functions
A function is introduced with the function keyword, names its outputs on the left of =, and is closed with end. The example below shows a single return value, MATLAB’s signature multiple return values, a default argument via nargin, and recursion.
Create a file named functions.m:
| |
Key things to notice:
- Outputs are named, not
returned. You assign to the output variable(s); MATLAB returns whatever they hold when the function ends. There is no value-bearingreturnstatement (a barereturnjust exits early). - Multiple outputs are listed in square brackets:
[s, p] = sum_and_product(...). The caller decides how many to capture. nargin(“number of arguments in”) reports how many arguments the caller actually passed, which is how you implement defaults.- Scope is local. Variables inside a function are private to it; they do not appear in the script’s workspace unless explicitly returned.
Anonymous Functions and Higher-Order Functions
MATLAB functions are first-class values through function handles. An anonymous function is created inline with the @(args) expression syntax and stored in a variable. Handles can capture surrounding variables and be passed to other functions — the foundation of higher-order programming in MATLAB.
Create a file named anonymous.m:
| |
Key things to notice:
@(x) x.^2defines an anonymous function of one argument. Using the element-wise operator.^(rather than^) means the same handle works on a scalar like5and on the vectorvalues.- Closures:
scalecaptures the value offactorat the moment the handle is created. Changingfactorafterward would not affectscale. - Higher-order functions:
apply_twiceaccepts another function as a parameter, andarrayfunapplies a handle to every element of an array — array-friendly alternatives to writing explicit loops.
Running with Docker
MATLAB is commercial software, so we use GNU Octave, a free, largely MATLAB-compatible alternative. The same .m files run in both.
| |
Expected Output
Running functions.m:
Area = 24
Sum = 9, Product = 20
9
27
5! = 120
Running anonymous.m:
25
70
81
1 4 9 16
Key Concepts
- Functions name their outputs. Assign to the output variable(s) declared after
function; there is no value-returningreturnstatement. - Native multiple return values. List outputs in brackets —
[s, p] = f(...)— and the caller chooses how many to capture, withnargoutavailable inside the function to detect the count. nargin/nargoutdrive flexible signatures. MATLAB has no built-in default-parameter syntax; you check the argument count and fill in defaults yourself.- Local functions live after the script code. A script file may define helper functions, but only below all the top-level statements (true in modern MATLAB and in Octave).
- Anonymous functions are first-class.
@(args) exprcreates a function handle that can be stored, passed, and called like any value, capturing surrounding variables as a closure. - Prefer array operations over loops. With element-wise operators (
.^,.*) and tools likearrayfun/cellfun, one function can process an entire matrix at once — the idiomatic MATLAB style. - Recursion works for naturally recursive problems like factorial, though MATLAB’s array operations are usually the faster, more idiomatic choice for bulk computation.
Running Today
All examples can be run using Docker:
docker pull gnuoctave/octave:9.4.0
Comments
Loading comments...
Leave a Comment