- publishing free software manuals
GNU Octave Manual Version 3
by John W. Eaton, David Bateman, Søren Hauberg
Paperback (6"x9"), 568 pages
ISBN 095461206X
RRP £24.95 ($39.95)

Get a printed copy>>>

11.4 Variable-length Return Lists

It is possible to return a variable number of output arguments from a function using a syntax that's similar to the one used with the varargin keyword. To let a function return a variable number of output arguments the varargout keyword is used. As with varargin, varargout is a cell array that will contain the requested output arguments.

As an example the following function sets the first output argument to 1, the second to 2, and so on.

function varargout = one_to_n ()
  for i = 1:nargout
    varargout{i} = i;
  endfor
endfunction

When called this function returns values like this

[a, b, c] = one_to_n ()
     => a =  1
     => b =  2
     => c =  3

Function File: [r1, r2, ..., rn] = deal (a)
Function File: [r1, r2, ..., rn] = deal (a1, a2, ..., an)

Copy the input parameters into the corresponding output parameters. If only one input parameter is supplied, its value is copied to each of the outputs.

For example,

[a, b, c] = deal (x, y, z);

is equivalent to

a = x;
b = y;
c = z;

and

[a, b, c] = deal (x);

is equivalent to

a = b = c = x;
ISBN 095461206XGNU Octave Manual Version 3See the print edition