nargout
Number of function output arguments
Syntax
Description
nargout
returns the number of function output arguments
specified in the call to the currently executing function. Use this syntax in the
body of a function only.
nargout(
returns the number of
outputs that appear in the fun
)fun
function definition. If the
function includes varargout
in its definition, then
nargout
returns the negative of the number of outputs. For
example, if function myFun
declares outputs y
,
z
, and varargout
, then
nargout('myFun')
returns -3
.
If fun
refers to a function that uses an arguments
validation block, then the returned value is the number of
declared positional arguments in the function definition as a non-negative value.
Examples
Input Arguments
Tips
When you use a function as part of an expression, such as an
if
statement, then MATLAB® calls the function with one output argument. Therefore, thenargout
function returns1
within expressions.If you check for a
nargout
value of 0 within a function and you specify the value of the output, MATLAB populatesans
. However, if you checknargout
and do not specify a value for the output, then MATLAB does not modifyans
.
Extended Capabilities
Version History
Introduced before R2006a