nargoutchk

Checks that a function was called with an acceptable number of output arguments.

Category: Programming > Functions

Syntax

nargoutchk(minargs, maxargs)

Errors if the calling function's nargout is below minargs or above maxargs.

Description

The check reads nargout of the function that calls nargoutchk, which tells how many results the caller asked for. It is useful for functions that only make sense when their result is captured, or that supply a fixed number of values.

If nargout is too small the error is Not enough output arguments.; if too large, Too many output arguments.

Input arguments

minargs
Smallest acceptable number of outputs requested.
maxargs
Largest acceptable number of outputs requested.

Example

Requiring both outputs

function [a, b] = pair()
    nargoutchk(2, 2);
    a = 1;
    b = 2;
end
[x, y] = pair()
try
    z = pair();
catch err
    disp(err.message)
end
Output
x = 1
y = 2
Not enough output arguments.

See also

narginchk nargchk