To me, a better justification (if there is one) for the nargout bump is to make it easier to write wrappers for the many builtin functions tht always return at least 1 argument, even when called with nargout=0. For example, at the command line, SORT always returns at least one output arg, even when one is not explicitly requested,
>> sort([5 1 3])
ans =
1 3 5
Suppose I now want to write a wrapper for sort() such that its 2nd argument returns 0-based indices instead of 1-based indices. Then I can do so, while preserving the above behavior, as easily as
function varargout=mysort(varargin)
[varargout{1:nargout}]=sort(varargin{:});
if nargout>1, varargout{2}=varargout{2}-1;
end
whereas without the bump, I would have to do something more complicated,
function varargout=mysort(varargin)
[varargout{1:max(1,nargout)}]=sort(varargin{:});
if nargout>1, varargout{2}=varargout{2}-1;
end
to ensure that at least one output arg was always returned. To my tastes, I would rather endure the second syntax for consistency's sake, but maybe that's just me...
Best Answer