A function in MATLAB is a block of code that does one job and can be reused anywhere in your scripts
Instead of writing the same calculation or operation over and over, you write it once as a function, give it a name, and call that name whenever you need it. MATLAB functions live in their own files or at the end of a script, take input values (called arguments), process them, and return output values back to you.
The basic structure is simple: you declare the function with the word function, list what it outputs, name the function, list what it takes as input, write the code that does the work, and end with end. A function that adds two numbers together, for example, would be four lines: the declaration, the addition, a return statement, and the closing end.
Key Takeaways
- Every function starts with the word function, followed by what it outputs, the function name, and what inputs it needs in parentheses.
- If you save a function in its own file, the filename must match the function name exactly, with a .m extension.
- Functions can return one output, multiple outputs, or no output at all — you decide what comes back.
- You call a function by typing its name and passing values in parentheses, the same way you call built-in MATLAB functions like sqrt() or sin().
- Variables created inside a function only exist inside that function — they do not affect or interfere with variables in your main script.
The anatomy of a simple function
A function declaration always begins with the word function. After that comes what the function will output (the return value or values), then the function name, then the inputs in parentheses. Here is a function that takes a single number and returns its square:
function output = square(x) output = x * x; end
The word output is the name of the variable that will be returned. The function name is square. The input is x. When someone calls square(5), MATLAB runs the code inside, multiplies 5 by 5, stores the result in output, and sends that value back.
If your function does not need to return anything — for example, a function that just prints a message — you can leave the output part blank. If it returns multiple values, list them in square brackets: function [output1, output2] = myfunction(input1, input2).
Saving a function in its own file
The most common way to organize functions is to save each one in its own file. The filename must match the function name exactly, and it must end in .m. If your function is named square, the file must be named square.m. MATLAB will not find the function if the names do not match.
Save the file in a folder that MATLAB can see. By default, MATLAB searches your current working directory (the folder shown at the top of the MATLAB window) and your Documents folder. If you save a function somewhere else, you need to add that folder to MATLAB's search path using the Set Path button in the Home tab, or by typing addpath('folder name') at the command line.
Once the file is saved in the right place with the right name, you can call the function from any script or from the command window just by typing its name and the inputs: result = square(7).
Functions with multiple inputs and outputs
A function can take as many inputs as you need, separated by commas. It can also return multiple values. Here is a function that takes two numbers and returns both their sum and their product:
function [sum_result, product_result] = sum_and_product(a, b) sum_result = a + b; product_result = a * b; end
When you call this function, you capture both outputs by listing them in square brackets: [s, p] = sum_and_product(3, 4). Now s contains 7 and p contains 12. You can also ignore one of the outputs if you do not need it: s = sum_and_product(3, 4) will work, but MATLAB will warn you that you are not using the second output.
The names you give to inputs and outputs inside the function do not have to match the names you use when you call it. MATLAB only cares about the order. If you call sum_and_product(10, 20), the value 10 goes into a and 20 goes into b, regardless of what you named your variables in the calling script.
Variable scope: what stays inside the function
Variables created inside a function exist only inside that function. If you create a variable called temp inside your function, it will not show up in your workspace or affect any variable named temp in your main script. This is called scope, and it keeps functions clean and prevents accidents.
The only way data gets in or out of a function is through the inputs and outputs you declare. If you need a value from your script to be used in the function, pass it as an input. If you need a result from the function, capture it in an output. This design makes functions predictable — you always know exactly what they need and what they will give back.
Calling functions from scripts and the command window
Once a function is saved and MATLAB can find it, you call it the same way you call built-in functions. Type the function name, put your inputs in parentheses, and optionally assign the output to a variable. If your function is named square and takes one input, you write result = square(9). If it returns multiple outputs, use square brackets: [s, p] = sum_and_product(5, 6).
You can call a function from the command window, from a script, or from another function. MATLAB does not care where the call comes from — it just runs the function code and returns the result. If MATLAB says "Undefined function or variable", it means the function file is not in a folder MATLAB can see, or the filename does not match the function name.
Common mistakes and how to avoid them
The most frequent error is a mismatch between the filename and the function name. If you write a function called calculate_average but save it as calc_avg.m, MATLAB will not find it. Always name the file exactly as you named the function.
Another common mistake is forgetting the end statement. Every function must close with end. If you leave it out, MATLAB will either give an error or treat the rest of your script as part of the function, which breaks everything that comes after.
A third mistake is trying to use a variable that was created inside the function in your main script. Variables inside a function are local — they disappear when the function finishes. If you need a value, make sure the function returns it as an output.
Frequently Asked Questions
Can I put multiple functions in one file?
Yes, but only the first function (the one at the top of the file) can have the same name as the file. The others are called local functions and can only be called from within that file. For most projects, it is cleaner to keep one function per file.
What if my function does not need any inputs?
Leave the parentheses empty. Write function output = my_function(). When you call it, type my_function() with nothing inside the parentheses.
How do I test a function I just wrote?
Save the file, then go to the command window and call the function with test values. For example, if you wrote the square function, type square(5) and press Enter. MATLAB will run it and show you the result. If you get an error, check that the filename matches the function name and that the file is in a folder MATLAB can see.
Can a function call another function?
Yes. A function can call any other function that MATLAB can find, including built-in functions and your own custom functions. This is useful for breaking complex tasks into smaller, reusable pieces.