File: Variable_002dlength-Argument-Lists.html

package info (click to toggle)
octave3.2 3.2.4-8
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 62,936 kB
  • ctags: 37,353
  • sloc: cpp: 219,497; fortran: 116,336; ansic: 10,264; sh: 5,508; makefile: 4,245; lex: 3,573; yacc: 3,062; objc: 2,042; lisp: 1,692; awk: 860; perl: 844
file content (129 lines) | stat: -rw-r--r-- 5,583 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<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.11">
<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">
<p>
<a name="Variable-length-Argument-Lists"></a>
<a name="Variable_002dlength-Argument-Lists"></a>
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>