Skip to main content
Logo image

Subsection Multiple Outputs

Functions can return multiple outputs. To specify multiple outputs, list them in square brackets in the function header. Here is an example of a function that computes both the perimeter and area of a rectangle given the lengths of its two sides.
Listing 18. MATLAB Function with 2 Inputs and 2 Outputs
function [perimeter, area] = rectangle_properties(side1, side2)

	% Use the inputs (side1 and side2) 
	% to compute the outputs (perimeter and area)
	perimeter = 2 * side1 + 2 * side2;
	area = side1 * side2;

end
To call this function and capture both outputs, use square brackets on the left side of the assignment. For example, to compute the perimeter and area of a rectangle with side lengths 4 and 7, type:
[p, a] = rectangle_perimeter(4, 7)	% returns p = 22 and a = 28
MATLAB will run the commands inside the function, using the input values you provided (4 and 7) to compute the output values (22 and 28). The results are then returned to the Command Window and stored in the variables p and a.

📝: Ignoring Outputs.