<html lang="en"> <head> <title>Multiple Return Values - 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="Defining-Functions.html#Defining-Functions" title="Defining Functions"> <link rel="next" href="Variable_002dlength-Argument-Lists.html#Variable_002dlength-Argument-Lists" title="Variable-length Argument Lists"> <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="Multiple-Return-Values"></a> <p> Next: <a rel="next" accesskey="n" href="Variable_002dlength-Argument-Lists.html#Variable_002dlength-Argument-Lists">Variable-length Argument Lists</a>, Previous: <a rel="previous" accesskey="p" href="Defining-Functions.html#Defining-Functions">Defining Functions</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.2 Multiple Return Values</h3> <p>Unlike many other computer languages, Octave allows you to define functions that return more than one value. The syntax for defining functions that return multiple values is <pre class="example"> function [<var>ret-list</var>] = <var>name</var> (<var>arg-list</var>) <var>body</var> endfunction </pre> <p class="noindent">where <var>name</var>, <var>arg-list</var>, and <var>body</var> have the same meaning as before, and <var>ret-list</var> is a comma-separated list of variable names that will hold the values returned from the function. The list of return values must have at least one element. If <var>ret-list</var> has only one element, this form of the <code>function</code> statement is equivalent to the form described in the previous section. <p>Here is an example of a function that returns two values, the maximum element of a vector and the index of its first occurrence in the vector. <pre class="example"> function [max, idx] = vmax (v) idx = 1; max = v (idx); for i = 2:length (v) if (v (i) > max) max = v (i); idx = i; endif endfor endfunction </pre> <p>In this particular case, the two values could have been returned as elements of a single array, but that is not always possible or convenient. The values to be returned may not have compatible dimensions, and it is often desirable to give the individual return values distinct names. <p>It is possible to use the <code>nthargout</code> function to obtain only some of the return values or several at once in a cell array. <a href="Cell-Array-Objects.html#Cell-Array-Objects">Cell Array Objects</a> <!-- nthargout scripts/general/nthargout.m --> <p><a name="doc_002dnthargout"></a> <div class="defun"> — Function File: <b>nthargout</b> (<var>n, func, <small class="dots">...</small></var>)<var><a name="index-nthargout-730"></a></var><br> — Function File: <b>nthargout</b> (<var>n, ntot, func, <small class="dots">...</small></var>)<var><a name="index-nthargout-731"></a></var><br> <blockquote><p>Return the <var>n</var>th output argument of function given by the function handle or string <var>func</var>. Any arguments after <var>func</var> are passed to <var>func</var>. The total number of arguments to call <var>func</var> with can be passed in <var>ntot</var>; by default <var>ntot</var> is <var>n</var>. The input <var>n</var> can also be a vector of indices of the output, in which case the output will be a cell array of the requested output arguments. <p>The intended use <code>nthargout</code> is to avoid intermediate variables. For example, when finding the indices of the maximum entry of a matrix, the following two compositions of nthargout <pre class="example"> <var>m</var> = magic (5); cell2mat (nthargout ([1, 2], @ind2sub, size(<var>m</var>), nthargout (2, @max, <var>m</var>(:)))) ⇒ 5 3 </pre> <p class="noindent">are completely equivalent to the following lines: <pre class="example"> <var>m</var> = magic(5); [~, idx] = max (<var>M</var>(:)); [i, j] = ind2sub (size (<var>m</var>), idx); [i, j] ⇒ 5 3 </pre> <p>It can also be helpful to have all output arguments in a single cell in the following manner: <pre class="example"> <var>USV</var> = nthargout ([1:3], @svd, hilb (5)); </pre> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dnargin.html#doc_002dnargin">nargin</a>, <a href="doc_002dnargout.html#doc_002dnargout">nargout</a>, <a href="doc_002dvarargin.html#doc_002dvarargin">varargin</a>, <a href="doc_002dvarargout.html#doc_002dvarargout">varargout</a>, <a href="doc_002disargout.html#doc_002disargout">isargout</a>. </p></blockquote></div> <p>In addition to setting <code>nargin</code> each time a function is called, Octave also automatically initializes <code>nargout</code> to the number of values that are expected to be returned. This allows you to write functions that behave differently depending on the number of values that the user of the function has requested. The implicit assignment to the built-in variable <code>ans</code> does not figure in the count of output arguments, so the value of <code>nargout</code> may be zero. <p>The <code>svd</code> and <code>lu</code> functions are examples of built-in functions that behave differently depending on the value of <code>nargout</code>. <p>It is possible to write functions that only set some return values. For example, calling the function <pre class="example"> function [x, y, z] = f () x = 1; z = 2; endfunction </pre> <p class="noindent">as <pre class="example"> [a, b, c] = f () </pre> <p class="noindent">produces: <pre class="example"> a = 1 b = [](0x0) c = 2 </pre> <p class="noindent">along with a warning. <!-- nargout src/ov-usr-fcn.cc --> <p><a name="doc_002dnargout"></a> <div class="defun"> — Built-in Function: <b>nargout</b> ()<var><a name="index-nargout-732"></a></var><br> — Built-in Function: <b>nargout</b> (<var>fcn_name</var>)<var><a name="index-nargout-733"></a></var><br> <blockquote><p>Within a function, return the number of values the caller expects to receive. If called with the optional argument <var>fcn_name</var>, return the maximum number of values the named function can produce, or -1 if the function can produce a variable number of values. <p>For example, <pre class="example"> f () </pre> <p class="noindent">will cause <code>nargout</code> to return 0 inside the function <code>f</code> and <pre class="example"> [s, t] = f () </pre> <p class="noindent">will cause <code>nargout</code> to return 2 inside the function <code>f</code>. <p>At the top level, <code>nargout</code> is undefined. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dnargin.html#doc_002dnargin">nargin</a>, <a href="doc_002dvarargin.html#doc_002dvarargin">varargin</a>, <a href="doc_002disargout.html#doc_002disargout">isargout</a>, <a href="doc_002dvarargout.html#doc_002dvarargout">varargout</a>, <a href="doc_002dnthargout.html#doc_002dnthargout">nthargout</a>. </p></blockquote></div> <p>It is good practice at the head of a function to verify that it has been called correctly. In Octave the following idiom is seen frequently <pre class="example"> if (nargin < min_#_inputs || nargin > max_#_inputs) print_usage (); endif </pre> <p class="noindent">which stops the function execution and prints a message about the correct way to call the function whenever the number of inputs is wrong. <p>For compatibility with <span class="sc">matlab</span>, <code>nargchk</code>, <code>narginchk</code> and <code>nargoutchk</code> are available which provide similar error checking. <!-- nargchk scripts/general/nargchk.m --> <p><a name="doc_002dnargchk"></a> <div class="defun"> — Function File: <var>msgstr</var> = <b>nargchk</b> (<var>minargs, maxargs, nargs</var>)<var><a name="index-nargchk-734"></a></var><br> — Function File: <var>msgstr</var> = <b>nargchk</b> (<var>minargs, maxargs, nargs, "string"</var>)<var><a name="index-nargchk-735"></a></var><br> — Function File: <var>msgstruct</var> = <b>nargchk</b> (<var>minargs, maxargs, nargs, "struct"</var>)<var><a name="index-nargchk-736"></a></var><br> <blockquote><p>Return an appropriate error message string (or structure) if the number of inputs requested is invalid. <p>This is useful for checking to see that the number of input arguments supplied to a function is within an acceptable range. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dnargoutchk.html#doc_002dnargoutchk">nargoutchk</a>, <a href="doc_002dnarginchk.html#doc_002dnarginchk">narginchk</a>, <a href="doc_002derror.html#doc_002derror">error</a>, <a href="doc_002dnargin.html#doc_002dnargin">nargin</a>, <a href="doc_002dnargout.html#doc_002dnargout">nargout</a>. </p></blockquote></div> <!-- narginchk scripts/general/narginchk.m --> <p><a name="doc_002dnarginchk"></a> <div class="defun"> — Function File: <b>narginchk</b> (<var>minargs, maxargs</var>)<var><a name="index-narginchk-737"></a></var><br> <blockquote><p>Check for correct number of arguments or generate an error message if the number of arguments in the calling function is outside the range <var>minargs</var> and <var>maxargs</var>. Otherwise, do nothing. <p>Both <var>minargs</var> and <var>maxargs</var> need to be scalar numeric values. Zero, Inf and negative values are all allowed, and <var>minargs</var> and <var>maxargs</var> may be equal. <p>Note that this function evaluates <code>nargin</code> on the caller. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dnargchk.html#doc_002dnargchk">nargchk</a>, <a href="doc_002dnargoutchk.html#doc_002dnargoutchk">nargoutchk</a>, <a href="doc_002derror.html#doc_002derror">error</a>, <a href="doc_002dnargout.html#doc_002dnargout">nargout</a>, <a href="doc_002dnargin.html#doc_002dnargin">nargin</a>. </p></blockquote></div> <!-- nargoutchk scripts/general/nargoutchk.m --> <p><a name="doc_002dnargoutchk"></a> <div class="defun"> — Function File: <b>nargoutchk</b> (<var>minargs, maxargs</var>)<var><a name="index-nargoutchk-738"></a></var><br> — Function File: <var>msgstr</var> = <b>nargoutchk</b> (<var>minargs, maxargs, nargs</var>)<var><a name="index-nargoutchk-739"></a></var><br> — Function File: <var>msgstr</var> = <b>nargoutchk</b> (<var>minargs, maxargs, nargs, "string"</var>)<var><a name="index-nargoutchk-740"></a></var><br> — Function File: <var>msgstruct</var> = <b>nargoutchk</b> (<var>minargs, maxargs, nargs, "struct"</var>)<var><a name="index-nargoutchk-741"></a></var><br> <blockquote><p>Check for correct number of output arguments. <p>On the first form, returns an error unless the number of arguments in its caller is between the values of <var>minargs</var> and <var>maxargs</var>. It does nothing otherwise. Note that this function evaluates the value of <code>nargout</code> on the caller so its value must have not been tampered with. <p>Both <var>minargs</var> and <var>maxargs</var> need to be a numeric scalar. Zero, Inf and negative are all valid, and they can have the same value. <p>For backward compatibility reasons, the other forms return an appropriate error message string (or structure) if the number of outputs requested is invalid. <p>This is useful for checking to see that the number of output arguments supplied to a function is within an acceptable range. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dnargchk.html#doc_002dnargchk">nargchk</a>, <a href="doc_002dnarginchk.html#doc_002dnarginchk">narginchk</a>, <a href="doc_002derror.html#doc_002derror">error</a>, <a href="doc_002dnargout.html#doc_002dnargout">nargout</a>, <a href="doc_002dnargin.html#doc_002dnargin">nargin</a>. </p></blockquote></div> <p><a name="doc_002dvarargin"></a><a name="doc_002dvarargout"></a> </body></html>