<html lang="en"> <head> <title>Variable-length Return Lists - GNU Octave</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Octave"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Functions-and-Scripts.html#Functions-and-Scripts" title="Functions and Scripts"> <link rel="prev" href="Ignoring-Arguments.html#Ignoring-Arguments" title="Ignoring Arguments"> <link rel="next" href="Returning-from-a-Function.html#Returning-from-a-Function" title="Returning from a Function"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Variable-length-Return-Lists"></a> <a name="Variable_002dlength-Return-Lists"></a> <p> Next: <a rel="next" accesskey="n" href="Returning-from-a-Function.html#Returning-from-a-Function">Returning from a Function</a>, Previous: <a rel="previous" accesskey="p" href="Ignoring-Arguments.html#Ignoring-Arguments">Ignoring Arguments</a>, Up: <a rel="up" accesskey="u" href="Functions-and-Scripts.html#Functions-and-Scripts">Functions and Scripts</a> <hr> </div> <h3 class="section">11.5 Variable-length Return Lists</h3> <p><a name="index-variable_002dlength-return-lists-747"></a><a name="index-g_t_0040code_007bvarargout_007d-748"></a> 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 special <code>varargin</code> parameter name. To let a function return a variable number of output arguments the special output parameter name <code>varargout</code> is used. As with <code>varargin</code>, <code>varargout</code> is a cell array that will contain the requested output arguments. <p>As an example the following function sets the first output argument to 1, the second to 2, and so on. <pre class="example"> function varargout = one_to_n () for i = 1:nargout varargout{i} = i; endfor endfunction </pre> <p class="noindent">When called this function returns values like this <pre class="example"> [a, b, c] = one_to_n () ⇒ a = 1 ⇒ b = 2 ⇒ c = 3 </pre> <p>If <code>varargin</code> (<code>varargout</code>) does not appear as the last element of the input (output) parameter list, then it is not special, and is handled the same as any other parameter name. <!-- deal scripts/general/deal.m --> <p><a name="doc_002ddeal"></a> <div class="defun"> — Function File: [<var>r1</var>, <var>r2</var>, <small class="dots">...</small>, <var>rn</var>] = <b>deal</b> (<var>a</var>)<var><a name="index-deal-749"></a></var><br> — Function File: [<var>r1</var>, <var>r2</var>, <small class="dots">...</small>, <var>rn</var>] = <b>deal</b> (<var>a1, a2, <small class="dots">...</small>, an</var>)<var><a name="index-deal-750"></a></var><br> <blockquote> <p>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. <p>For example, <pre class="example"> [a, b, c] = deal (x, y, z); </pre> <p class="noindent">is equivalent to <pre class="example"> a = x; b = y; c = z; </pre> <p class="noindent">and <pre class="example"> [a, b, c] = deal (x); </pre> <p class="noindent">is equivalent to <pre class="example"> a = b = c = x; </pre> </blockquote></div> </body></html>