This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Number of function output arguments





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(fun) returns the number of outputs that appear in the 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.


collapse all

Create a function in a file named subtract.m that calculates a second return value, absdif, only if requested.

function [dif,absdif] = subtract(y,x)
dif = y-x;
if nargout > 1
    disp('Calculating absolute value')
    absdif = abs(dif);

At the command prompt, call the subtract function with one return value.

diff = subtract(42,13)
diff =


Call the subtract function again with two return values.

[dif,absdif] = subtract(2,5)
Calculating absolute value

dif =


absdif =


Determine how many outputs a function can return.

The function subtract created in the previous example has two outputs in its declaration statement (dif and absdif).

fun = @subtract;
ans =

Determine how many outputs a function that uses varargout can return.

Define a function in a file named mySize.m that returns a vector of dimensions from the size function and the individual dimensions using varargout.

function [sizeVector,varargout] = mySize(x)
   sizeVector = size(x);
   varargout = cell(1,nargout-1);
   for k = 1:length(varargout)
      varargout{k} = sizeVector(k);

At the command line, query how many outputs mySize can return.

fun = 'mySize';
ans =

The minus sign indicates that the second output is varargout. The mySize function can return an indeterminate number of additional outputs.

Input Arguments

collapse all

Function to return number of output arguments from its definition, specified as a function handle or a character vector.

Example: @rand

Example: 'sortrows'

Data Types: char | function_handle


  • When you use a function as part of an expression, such as an if statement, MATLAB® calls the function with one output argument. Therefore, the nargout function returns 1 within expressions.

  • If nargout == 0 and you specify the value of the output, MATLAB populates ans. However, if you check nargout and do not specify a value for the output, MATLAB does not modify ans.

Extended Capabilities

Introduced before R2006a

Was this topic helpful?