<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Variable-length Return Lists (GNU Octave (version 5.1.0))</title> <meta name="description" content="Variable-length Return Lists (GNU Octave (version 5.1.0))"> <meta name="keywords" content="Variable-length Return Lists (GNU Octave (version 5.1.0))"> <meta name="resource-type" content="document"> <meta name="distribution" content="global"> <meta name="Generator" content="makeinfo"> <link href="index.html#Top" rel="start" title="Top"> <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index"> <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> <link href="Functions-and-Scripts.html#Functions-and-Scripts" rel="up" title="Functions and Scripts"> <link href="Returning-from-a-Function.html#Returning-from-a-Function" rel="next" title="Returning from a Function"> <link href="Ignoring-Arguments.html#Ignoring-Arguments" rel="prev" title="Ignoring Arguments"> <style type="text/css"> <!-- a.summary-letter {text-decoration: none} blockquote.indentedblock {margin-right: 0em} blockquote.smallindentedblock {margin-right: 0em; font-size: smaller} blockquote.smallquotation {font-size: smaller} div.display {margin-left: 3.2em} div.example {margin-left: 3.2em} div.lisp {margin-left: 3.2em} div.smalldisplay {margin-left: 3.2em} div.smallexample {margin-left: 3.2em} div.smalllisp {margin-left: 3.2em} kbd {font-style: oblique} pre.display {font-family: inherit} pre.format {font-family: inherit} pre.menu-comment {font-family: serif} pre.menu-preformatted {font-family: serif} pre.smalldisplay {font-family: inherit; font-size: smaller} pre.smallexample {font-size: smaller} pre.smallformat {font-family: inherit; font-size: smaller} pre.smalllisp {font-size: smaller} span.nolinebreak {white-space: nowrap} span.roman {font-family: initial; font-weight: normal} span.sansserif {font-family: sans-serif; font-weight: normal} ul.no-bullet {list-style: none} --> </style> <link rel="stylesheet" type="text/css" href="octave.css"> </head> <body lang="en"> <a name="Variable_002dlength-Return-Lists"></a> <div class="header"> <p> Next: <a href="Returning-from-a-Function.html#Returning-from-a-Function" accesskey="n" rel="next">Returning from a Function</a>, Previous: <a href="Ignoring-Arguments.html#Ignoring-Arguments" accesskey="p" rel="prev">Ignoring Arguments</a>, Up: <a href="Functions-and-Scripts.html#Functions-and-Scripts" accesskey="u" rel="up">Functions and Scripts</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p> </div> <hr> <a name="Variable_002dlength-Return-Lists-1"></a> <h3 class="section">11.6 Variable-length Return Lists</h3> <a name="index-variable_002dlength-return-lists"></a> <a name="index-varargout"></a> <p>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> <p>As an example the following function sets the first output argument to 1, the second to 2, and so on. </p> <div class="example"> <pre class="example">function varargout = one_to_n () for i = 1:nargout varargout{i} = i; endfor endfunction </pre></div> <p>When called this function returns values like this </p> <div class="example"> <pre class="example">[a, b, c] = one_to_n () ⇒ a = 1 ⇒ b = 2 ⇒ c = 3 </pre></div> <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. </p> <a name="XREFdeal"></a><dl> <dt><a name="index-deal"></a><em>[<var>r1</var>, <var>r2</var>, …, <var>rn</var>] =</em> <strong>deal</strong> <em>(<var>a</var>)</em></dt> <dt><a name="index-deal-1"></a><em>[<var>r1</var>, <var>r2</var>, …, <var>rn</var>] =</em> <strong>deal</strong> <em>(<var>a1</var>, <var>a2</var>, …, <var>an</var>)</em></dt> <dd> <p>Copy the input parameters into the corresponding output parameters. </p> <p>If only a single input parameter is supplied, its value is copied to each of the outputs. </p> <p>For example, </p> <div class="example"> <pre class="example">[a, b, c] = deal (x, y, z); </pre></div> <p>is equivalent to </p> <div class="example"> <pre class="example">a = x; b = y; c = z; </pre></div> <p>and </p> <div class="example"> <pre class="example">[a, b, c] = deal (x); </pre></div> <p>is equivalent to </p> <div class="example"> <pre class="example">a = b = c = x; </pre></div> <p>Programming Note: <code>deal</code> is often used with comma separated lists derived from cell arrays or structures. This is unnecessary as the interpreter can perform the same action without the overhead of a function call. For example: </p> <div class="example"> <pre class="example">c = {[1 2], "Three", 4}; [x, y, z] = c{:} ⇒ x = 1 2 y = Three z = 4 </pre></div> <p><strong>See also:</strong> <a href="Processing-Data-in-Cell-Arrays.html#XREFcell2struct">cell2struct</a>, <a href="Processing-Data-in-Structures.html#XREFstruct2cell">struct2cell</a>, <a href="Special-Utility-Matrices.html#XREFrepmat">repmat</a>. </p></dd></dl> <hr> <div class="header"> <p> Next: <a href="Returning-from-a-Function.html#Returning-from-a-Function" accesskey="n" rel="next">Returning from a Function</a>, Previous: <a href="Ignoring-Arguments.html#Ignoring-Arguments" accesskey="p" rel="prev">Ignoring Arguments</a>, Up: <a href="Functions-and-Scripts.html#Functions-and-Scripts" accesskey="u" rel="up">Functions and Scripts</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p> </div> </body> </html>