<html lang="en"> <head> <title>Variable-length Argument 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="Multiple-Return-Values.html#Multiple-Return-Values" title="Multiple Return Values"> <link rel="next" href="Ignoring-Arguments.html#Ignoring-Arguments" title="Ignoring Arguments"> <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-Argument-Lists"></a> <a name="Variable_002dlength-Argument-Lists"></a> <p> Next: <a rel="next" accesskey="n" href="Ignoring-Arguments.html#Ignoring-Arguments">Ignoring Arguments</a>, Previous: <a rel="previous" accesskey="p" href="Multiple-Return-Values.html#Multiple-Return-Values">Multiple Return Values</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.3 Variable-length Argument Lists</h3> <p><a name="index-variable_002dlength-argument-lists-742"></a><a name="index-g_t_0040code_007bvarargin_007d-743"></a> Sometimes the number of input arguments is not known when the function is defined. As an example think of a function that returns the smallest of all its input arguments. For example: <pre class="example"> a = smallest (1, 2, 3); b = smallest (1, 2, 3, 4); </pre> <p class="noindent">In this example both <code>a</code> and <code>b</code> would be 1. One way to write the <code>smallest</code> function is <pre class="example"> function val = smallest (arg1, arg2, arg3, arg4, arg5) <var>body</var> endfunction </pre> <p class="noindent">and then use the value of <code>nargin</code> to determine which of the input arguments should be considered. The problem with this approach is that it can only handle a limited number of input arguments. <p>If the special parameter name <code>varargin</code> appears at the end of a function parameter list it indicates that the function takes a variable number of input arguments. Using <code>varargin</code> the function looks like this <pre class="example"> function val = smallest (varargin) <var>body</var> endfunction </pre> <p class="noindent">In the function body the input arguments can be accessed through the variable <code>varargin</code>. This variable is a cell array containing all the input arguments. See <a href="Cell-Arrays.html#Cell-Arrays">Cell Arrays</a>, for details on working with cell arrays. The <code>smallest</code> function can now be defined like this <pre class="example"> function val = smallest (varargin) val = min ([varargin{:}]); endfunction </pre> <p class="noindent">This implementation handles any number of input arguments, but it's also a very simple solution to the problem. <p>A slightly more complex example of <code>varargin</code> is a function <code>print_arguments</code> that prints all input arguments. Such a function can be defined like this <pre class="example"> function print_arguments (varargin) for i = 1:length (varargin) printf ("Input argument %d: ", i); disp (varargin{i}); endfor endfunction </pre> <p class="noindent">This function produces output like this <pre class="example"> print_arguments (1, "two", 3); -| Input argument 1: 1 -| Input argument 2: two -| Input argument 3: 3 </pre> <!-- parseparams scripts/miscellaneous/parseparams.m --> <p><a name="doc_002dparseparams"></a> <div class="defun"> — Function File: [<var>reg</var>, <var>prop</var>] = <b>parseparams</b> (<var>params</var>)<var><a name="index-parseparams-744"></a></var><br> — Function File: [<var>reg</var>, <var>var1</var>, <small class="dots">...</small>] = <b>parseparams</b> (<var>params, name1, default1, <small class="dots">...</small></var>)<var><a name="index-parseparams-745"></a></var><br> <blockquote><p>Return in <var>reg</var> the cell elements of <var>param</var> up to the first string element and in <var>prop</var> all remaining elements beginning with the first string element. For example: <pre class="example"> [reg, prop] = parseparams ({1, 2, "linewidth", 10}) reg = { [1,1] = 1 [1,2] = 2 } prop = { [1,1] = linewidth [1,2] = 10 } </pre> <p>The parseparams function may be used to separate 'regular' arguments and additional arguments given as property/value pairs of the <var>varargin</var> cell array. <p>In the second form of the call, available options are specified directly with their default values given as name-value pairs. If <var>params</var> do not form name-value pairs, or if an option occurs that does not match any of the available options, an error occurs. When called from an m-file function, the error is prefixed with the name of the caller function. The matching of options is case-insensitive. <!-- 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_002dvarargin.html#doc_002dvarargin">varargin</a>. </p></blockquote></div> </body></html>