Introduction
Sometimes you want a function to give back more than one answer at the same time. Multiple outputs let you do that easily.
function [out1, out2, ..., outN] = functionName(inputs)
% Your code here
endfunction [sum, diff] = addAndSubtract(a, b) sum = a + b; diff = a - b; end
[s, d] = addAndSubtract(5, 3) % s will be 8, d will be 2
[onlySum] = addAndSubtract(5, 3) % onlySum will be 8, difference is ignored
function [area, perimeter] = rectangleProperties(length, width)
area = length * width;
perimeter = 2 * (length + width);
end
% Call the function
[lArea, lPerimeter] = rectangleProperties(4, 7);
% Display the results
fprintf('Area: %d\n', lArea);
fprintf('Perimeter: %d\n', lPerimeter);myFunc that returns two outputs?function [sumVal, diffVal] = calc(a, b)
sumVal = a + b;
diffVal = a - b;
end
[x, y] = calc(5, 3);x and y?function [a, b] = wrongFunc(x)
a = x * 2;
b = x + 3
endstats that takes a vector and returns three outputs: the mean, median, and standard deviation. Which function header and call are correct?Pass the test and this pattern moves unknown