Writing Your Own Functions
A function lives in its own block (or, for a real project, its own .m file) and lets you package up a calculation you'll reuse.
function result = square(x)
result = x^2;
end
disp(square(5))
Multiple inputs and outputs
function [total, average] = stats(v)
total = sum(v);
average = mean(v);
end
[t, a] = stats([1, 2, 3, 4]);
disp(t)
disp(a)
Why This Matters for Your Exams
A function is how you avoid retyping the same five lines for every new set of numbers a question gives you -- write it once, call it on whatever input the question provides.
Try it yourself
lock
Real-time practice is a paid add-on
Reading units and the practice quiz are free. Pay once to unlock running real code for LaTeX & MATLAB.

