In the body of a function nargin
and nargout
indicate respectively the actual number of input and output supplied in the call.
We can for example control the execution of a function based on the number of provided input.
myVector.m:
function [res] = myVector(a, b, c)
% Roughly emulates the colon operator
switch nargin
case 1
res = [0:a];
case 2
res = [a:b];
case 3
res = [a:b:c];
otherwise
error('Wrong number of params');
end
end
terminal:
>> myVector(10)
ans =
0 1 2 3 4 5 6 7 8 9 10
>> myVector(10, 20)
ans =
10 11 12 13 14 15 16 17 18 19 20
>> myVector(10, 2, 20)
ans =
10 12 14 16 18 20
In a similar way we can control the execution of a function based on the number of output parameters.
myIntegerDivision:
function [qt, rm] = myIntegerDivision(a, b)
qt = floor(a / b);
if nargout == 2
rm = rem(a, b);
end
end
terminal:
>> q = myIntegerDivision(10, 7)
q = 1
>> [q, r] = myIntegerDivision(10, 7)
q = 1
r = 3