Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > 3e60ff9d4d6f58c8fbd17208f14089fa > files > 441

octave-doc-3.2.3-3mdv2010.0.i586.rpm

<html lang="en">
<head>
<title>Variable-length Argument Lists - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<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="Variable_002dlength-Return-Lists.html#Variable_002dlength-Return-Lists" title="Variable-length Return 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="Variable-length-Argument-Lists"></a>
<a name="Variable_002dlength-Argument-Lists"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Variable_002dlength-Return-Lists.html#Variable_002dlength-Return-Lists">Variable-length Return Lists</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Multiple-Return-Values.html#Multiple-Return-Values">Multiple Return Values</a>,
Up:&nbsp;<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-603"></a><a name="index-g_t_0040code_007bvarargin_007d-604"></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>
   <!-- ./miscellaneous/parseparams.m -->
   <p><a name="doc_002dparseparams"></a>

<div class="defun">
&mdash; Function File: [<var>reg</var>, <var>prop</var>] = <b>parseparams</b> (<var>params</var>)<var><a name="index-parseparams-605"></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. 
<!-- 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>