annotate doc/interpreter/func.txi @ 8074:595028fcf65d

doc fix
author John W. Eaton <jwe@octave.org>
date Thu, 28 Aug 2008 15:31:01 -0400
parents 3b6e7d04b614
children 06094fa570a3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6778
083721ae3dfa [project @ 2007-07-18 17:03:10 by jwe]
jwe
parents: 6638
diff changeset
1 @c Copyright (C) 1996, 1997, 2007 John W. Eaton
7018
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
2 @c
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
3 @c This file is part of Octave.
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
4 @c
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
5 @c Octave is free software; you can redistribute it and/or modify it
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
6 @c under the terms of the GNU General Public License as published by the
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
7 @c Free Software Foundation; either version 3 of the License, or (at
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
8 @c your option) any later version.
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
9 @c
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
10 @c Octave is distributed in the hope that it will be useful, but WITHOUT
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
11 @c ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
12 @c FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
13 @c for more details.
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
14 @c
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
15 @c You should have received a copy of the GNU General Public License
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
16 @c along with Octave; see the file COPYING. If not, see
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
17 @c <http://www.gnu.org/licenses/>.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
18
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 4029
diff changeset
19 @node Functions and Scripts
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
20 @chapter Functions and Script Files
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
21 @cindex defining functions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
22 @cindex user-defined functions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
23 @cindex functions, user-defined
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
24 @cindex script files
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
25
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
26 Complicated Octave programs can often be simplified by defining
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
27 functions. Functions can be defined directly on the command line during
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
28 interactive Octave sessions, or in external files, and can be called just
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
29 like built-in functions.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
30
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
31 @menu
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
32 * Defining Functions::
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
33 * Multiple Return Values::
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
34 * Variable-length Argument Lists::
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
35 * Variable-length Return Lists::
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
36 * Returning From a Function::
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
37 * Default Arguments::
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
38 * Function Files::
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
39 * Script Files::
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
40 * Function Handles Inline Functions and Anonymous Functions::
6549
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
41 * Commands::
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
42 * Organization of Functions::
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
43 @end menu
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
44
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 4029
diff changeset
45 @node Defining Functions
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
46 @section Defining Functions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
47 @cindex @code{function} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
48 @cindex @code{endfunction} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
49
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
50 In its simplest form, the definition of a function named @var{name}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
51 looks like this:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
52
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
53 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
54 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
55 function @var{name}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
56 @var{body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
57 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
58 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
59 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
60
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
61 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
62 A valid function name is like a valid variable name: a sequence of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
63 letters, digits and underscores, not starting with a digit. Functions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
64 share the same pool of names as variables.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
65
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
66 The function @var{body} consists of Octave statements. It is the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
67 most important part of the definition, because it says what the function
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
68 should actually @emph{do}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
69
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
70 For example, here is a function that, when executed, will ring the bell
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
71 on your terminal (assuming that it is possible to do so):
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
72
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
73 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
74 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
75 function wakeup
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
76 printf ("\a");
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
77 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
78 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
79 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
80
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
81 The @code{printf} statement (@pxref{Input and Output}) simply tells
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
82 Octave to print the string @code{"\a"}. The special character @samp{\a}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
83 stands for the alert character (ASCII 7). @xref{Strings}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
84
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
85 Once this function is defined, you can ask Octave to evaluate it by
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
86 typing the name of the function.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
87
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
88 Normally, you will want to pass some information to the functions you
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
89 define. The syntax for passing parameters to a function in Octave is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
90
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
91 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
92 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
93 function @var{name} (@var{arg-list})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
94 @var{body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
95 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
96 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
97 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
98
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
99 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
100 where @var{arg-list} is a comma-separated list of the function's
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
101 arguments. When the function is called, the argument names are used to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
102 hold the argument values given in the call. The list of arguments may
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
103 be empty, in which case this form is equivalent to the one shown above.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
104
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
105 To print a message along with ringing the bell, you might modify the
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
106 @code{wakeup} to look like this:
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
107
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
108 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
109 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
110 function wakeup (message)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
111 printf ("\a%s\n", message);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
112 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
113 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
114 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
115
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
116 Calling this function using a statement like this
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
117
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
118 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
119 wakeup ("Rise and shine!");
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
120 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
121
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
122 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
123 will cause Octave to ring your terminal's bell and print the message
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
124 @samp{Rise and shine!}, followed by a newline character (the @samp{\n}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
125 in the first argument to the @code{printf} statement).
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
126
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
127 In most cases, you will also want to get some information back from the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
128 functions you define. Here is the syntax for writing a function that
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
129 returns a single value:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
130
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
131 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
132 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
133 function @var{ret-var} = @var{name} (@var{arg-list})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
134 @var{body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
135 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
136 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
137 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
138
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
139 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
140 The symbol @var{ret-var} is the name of the variable that will hold the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
141 value to be returned by the function. This variable must be defined
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
142 before the end of the function body in order for the function to return
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
143 a value.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
144
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
145 Variables used in the body of a function are local to the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
146 function. Variables named in @var{arg-list} and @var{ret-var} are also
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
147 local to the function. @xref{Global Variables}, for information about
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
148 how to access global variables inside a function.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
149
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
150 For example, here is a function that computes the average of the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
151 elements of a vector:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
152
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
153 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
154 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
155 function retval = avg (v)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
156 retval = sum (v) / length (v);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
157 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
158 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
159 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
160
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
161 If we had written @code{avg} like this instead,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
162
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
163 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
164 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
165 function retval = avg (v)
4029
2cc57b6169cf [project @ 2002-08-09 07:36:15 by jwe]
jwe
parents: 3760
diff changeset
166 if (isvector (v))
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
167 retval = sum (v) / length (v);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
168 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
169 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
170 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
171 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
172
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
173 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
174 and then called the function with a matrix instead of a vector as the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
175 argument, Octave would have printed an error message like this:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
176
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
177 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
178 @group
8015
30629059b72d Update the manual to reflect the changes in error output
sh@sh-laptop
parents: 7984
diff changeset
179 error: value on right hand side of assignment is undefined
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
180 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
181 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
182
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
183 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
184 because the body of the @code{if} statement was never executed, and
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
185 @code{retval} was never defined. To prevent obscure errors like this,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
186 it is a good idea to always make sure that the return variables will
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
187 always have values, and to produce meaningful error messages when
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
188 problems are encountered. For example, @code{avg} could have been
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
189 written like this:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
190
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
191 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
192 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
193 function retval = avg (v)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
194 retval = 0;
4029
2cc57b6169cf [project @ 2002-08-09 07:36:15 by jwe]
jwe
parents: 3760
diff changeset
195 if (isvector (v))
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
196 retval = sum (v) / length (v);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
197 else
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
198 error ("avg: expecting vector argument");
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
199 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
200 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
201 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
202 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
203
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
204 There is still one additional problem with this function. What if it is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
205 called without an argument? Without additional error checking, Octave
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
206 will probably print an error message that won't really help you track
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
207 down the source of the error. To allow you to catch errors like this,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
208 Octave provides each function with an automatic variable called
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
209 @code{nargin}. Each time a function is called, @code{nargin} is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
210 automatically initialized to the number of arguments that have actually
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
211 been passed to the function. For example, we might rewrite the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
212 @code{avg} function like this:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
213
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
214 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
215 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
216 function retval = avg (v)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
217 retval = 0;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
218 if (nargin != 1)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
219 usage ("avg (vector)");
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
220 endif
4029
2cc57b6169cf [project @ 2002-08-09 07:36:15 by jwe]
jwe
parents: 3760
diff changeset
221 if (isvector (v))
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
222 retval = sum (v) / length (v);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
223 else
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
224 error ("avg: expecting vector argument");
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
225 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
226 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
227 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
228 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
229
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
230 Although Octave does not automatically report an error if you call a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
231 function with more arguments than expected, doing so probably indicates
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
232 that something is wrong. Octave also does not automatically report an
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
233 error if a function is called with too few arguments, but any attempt to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
234 use a variable that has not been given a value will result in an error.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
235 To avoid such problems and to provide useful messages, we check for both
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
236 possibilities and issue our own error message.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
237
4700
ca3a1d687bba [project @ 2004-01-21 03:28:31 by jwe]
jwe
parents: 4466
diff changeset
238 @DOCSTRING(nargin)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
239
6558
e0e1c6df5ab2 [project @ 2007-04-20 19:33:24 by jwe]
jwe
parents: 6556
diff changeset
240 @DOCSTRING(inputname)
e0e1c6df5ab2 [project @ 2007-04-20 19:33:24 by jwe]
jwe
parents: 6556
diff changeset
241
3371
86873384cd10 [project @ 1999-11-21 17:31:07 by jwe]
jwe
parents: 3294
diff changeset
242 @DOCSTRING(silent_functions)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
243
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 4029
diff changeset
244 @node Multiple Return Values
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
245 @section Multiple Return Values
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
246
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
247 Unlike many other computer languages, Octave allows you to define
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
248 functions that return more than one value. The syntax for defining
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
249 functions that return multiple values is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
250
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
251 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
252 function [@var{ret-list}] = @var{name} (@var{arg-list})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
253 @var{body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
254 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
255 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
256
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
257 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
258 where @var{name}, @var{arg-list}, and @var{body} have the same meaning
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
259 as before, and @var{ret-list} is a comma-separated list of variable
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
260 names that will hold the values returned from the function. The list of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
261 return values must have at least one element. If @var{ret-list} has
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
262 only one element, this form of the @code{function} statement is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
263 equivalent to the form described in the previous section.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
264
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
265 Here is an example of a function that returns two values, the maximum
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
266 element of a vector and the index of its first occurrence in the vector.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
267
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
268 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
269 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
270 function [max, idx] = vmax (v)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
271 idx = 1;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
272 max = v (idx);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
273 for i = 2:length (v)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
274 if (v (i) > max)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
275 max = v (i);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
276 idx = i;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
277 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
278 endfor
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
279 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
280 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
281 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
282
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
283 In this particular case, the two values could have been returned as
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
284 elements of a single array, but that is not always possible or
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
285 convenient. The values to be returned may not have compatible
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
286 dimensions, and it is often desirable to give the individual return
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
287 values distinct names.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
288
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
289 In addition to setting @code{nargin} each time a function is called,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
290 Octave also automatically initializes @code{nargout} to the number of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
291 values that are expected to be returned. This allows you to write
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
292 functions that behave differently depending on the number of values that
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
293 the user of the function has requested. The implicit assignment to the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
294 built-in variable @code{ans} does not figure in the count of output
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
295 arguments, so the value of @code{nargout} may be zero.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
296
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
297 The @code{svd} and @code{lu} functions are examples of built-in
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
298 functions that behave differently depending on the value of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
299 @code{nargout}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
300
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
301 It is possible to write functions that only set some return values. For
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
302 example, calling the function
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
303
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
304 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
305 function [x, y, z] = f ()
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
306 x = 1;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
307 z = 2;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
308 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
309 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
310
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
311 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
312 as
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
313
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
314 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
315 [a, b, c] = f ()
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
316 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
317
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
318 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
319 produces:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
320
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
321 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
322 a = 1
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
323
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
324 b = [](0x0)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
325
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
326 c = 2
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
327 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
328
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
329 @noindent
6501
68f3125f6e27 [project @ 2007-04-05 20:53:40 by jwe]
jwe
parents: 6477
diff changeset
330 along with a warning.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
331
4700
ca3a1d687bba [project @ 2004-01-21 03:28:31 by jwe]
jwe
parents: 4466
diff changeset
332 @DOCSTRING(nargout)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
333
3371
86873384cd10 [project @ 1999-11-21 17:31:07 by jwe]
jwe
parents: 3294
diff changeset
334 @DOCSTRING(nargchk)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
335
7984
bbaa5d7d0143 Some documentation updates
David Bateman <dbateman@free.fr>
parents: 7638
diff changeset
336 @DOCSTRING(nargoutchk)
bbaa5d7d0143 Some documentation updates
David Bateman <dbateman@free.fr>
parents: 7638
diff changeset
337
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 4029
diff changeset
338 @node Variable-length Argument Lists
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
339 @section Variable-length Argument Lists
4933
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
340 @cindex variable-length argument lists
8074
595028fcf65d doc fix
John W. Eaton <jwe@octave.org>
parents: 8072
diff changeset
341 @vrindex @code{varargin}
8072
3b6e7d04b614 doc fixes
John W. Eaton <jwe@octave.org>
parents: 8015
diff changeset
342
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
343 Sometimes the number of input arguments is not known when the function
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
344 is defined. As an example think of a function that returns the smallest
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
345 of all its input arguments. For example,
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
346
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
347 @example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
348 a = smallest (1, 2, 3);
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
349 b = smallest (1, 2, 3, 4);
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
350 @end example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
351
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
352 @noindent
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
353 In this example both @code{a} and @code{b} would be 1. One way to write
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
354 the @code{smallest} function is
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
355
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
356 @example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
357 function val = smallest (arg1, arg2, arg3, arg4, arg5)
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
358 @var{body}
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
359 endfunction
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
360 @end example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
361
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
362 @noindent
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
363 and then use the value of @code{nargin} to determine which of the input
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
364 arguments should be considered. The problem with this approach is
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
365 that it can only handle a limited number of input arguments.
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
366
7588
cbedf652a752 doc fix for varargin and varargout change
John W. Eaton <jwe@octave.org>
parents: 7031
diff changeset
367 If the special parameter name @code{varargin} appears at the end of a
cbedf652a752 doc fix for varargin and varargout change
John W. Eaton <jwe@octave.org>
parents: 7031
diff changeset
368 function parameter list it indicates that the function takes a variable
cbedf652a752 doc fix for varargin and varargout change
John W. Eaton <jwe@octave.org>
parents: 7031
diff changeset
369 number of input arguments. Using @code{varargin} the function
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
370 looks like this
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
371
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
372 @example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
373 function val = smallest (varargin)
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
374 @var{body}
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
375 endfunction
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
376 @end example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
377
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
378 @noindent
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
379 In the function body the input arguments can be accessed through the
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
380 variable @code{varargin}. This variable is a cell array containing
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
381 all the input arguments. @xref{Cell Arrays}, for details on working
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
382 with cell arrays. The @code{smallest} function can now be defined
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
383 like this
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
384
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
385 @example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
386 function val = smallest (varargin)
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
387 val = min ([varargin@{:@}]);
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
388 endfunction
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
389 @end example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
390
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
391 @noindent
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
392 This implementation handles any number of input arguments, but it's also
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
393 a very simple solution to the problem.
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
394
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
395 A slightly more complex example of @code{varargin} is a function
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
396 @code{print_arguments} that prints all input arguments. Such a function
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
397 can be defined like this
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
398
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
399 @example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
400 function print_arguments (varargin)
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
401 for i = 1:length (varargin)
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
402 printf ("Input argument %d: ", i);
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
403 disp (varargin@{i@});
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
404 endfor
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
405 endfunction
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
406 @end example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
407
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
408 @noindent
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
409 This function produces output like this
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
410
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
411 @example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
412 @group
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
413 print_arguments (1, "two", 3);
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
414 @print{} Input argument 1: 1
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
415 @print{} Input argument 2: two
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
416 @print{} Input argument 3: 3
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
417 @end group
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
418 @end example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
419
6558
e0e1c6df5ab2 [project @ 2007-04-20 19:33:24 by jwe]
jwe
parents: 6556
diff changeset
420 @DOCSTRING(parseparams)
e0e1c6df5ab2 [project @ 2007-04-20 19:33:24 by jwe]
jwe
parents: 6556
diff changeset
421
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 4029
diff changeset
422 @node Variable-length Return Lists
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
423 @section Variable-length Return Lists
4933
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
424 @cindex variable-length return lists
8074
595028fcf65d doc fix
John W. Eaton <jwe@octave.org>
parents: 8072
diff changeset
425 @vrindex @code{varargout}
8072
3b6e7d04b614 doc fixes
John W. Eaton <jwe@octave.org>
parents: 8015
diff changeset
426
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
427 It is possible to return a variable number of output arguments from a
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
428 function using a syntax that's similar to the one used with the
7588
cbedf652a752 doc fix for varargin and varargout change
John W. Eaton <jwe@octave.org>
parents: 7031
diff changeset
429 special @code{varargin} parameter name. To let a function return a
cbedf652a752 doc fix for varargin and varargout change
John W. Eaton <jwe@octave.org>
parents: 7031
diff changeset
430 variable number of output arguments the special output parameter name
cbedf652a752 doc fix for varargin and varargout change
John W. Eaton <jwe@octave.org>
parents: 7031
diff changeset
431 @code{varargout} is used. As with @code{varargin}, @code{varargout} is
cbedf652a752 doc fix for varargin and varargout change
John W. Eaton <jwe@octave.org>
parents: 7031
diff changeset
432 a cell array that will contain the requested output arguments.
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
433
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
434 As an example the following function sets the first output argument to
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
435 1, the second to 2, and so on.
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
436
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
437 @example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
438 function varargout = one_to_n ()
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
439 for i = 1:nargout
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
440 varargout@{i@} = i;
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
441 endfor
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
442 endfunction
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
443 @end example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
444
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
445 @noindent
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
446 When called this function returns values like this
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
447
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
448 @example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
449 @group
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
450 [a, b, c] = one_to_n ()
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
451 @result{} a = 1
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
452 @result{} b = 2
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
453 @result{} c = 3
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
454 @end group
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
455 @end example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
456
7588
cbedf652a752 doc fix for varargin and varargout change
John W. Eaton <jwe@octave.org>
parents: 7031
diff changeset
457 If @code{varargin} (@code{varargout}) does not appear as the last
cbedf652a752 doc fix for varargin and varargout change
John W. Eaton <jwe@octave.org>
parents: 7031
diff changeset
458 element of the input (output) parameter list, then it is not special,
cbedf652a752 doc fix for varargin and varargout change
John W. Eaton <jwe@octave.org>
parents: 7031
diff changeset
459 and is handled the same as any other parameter name.
cbedf652a752 doc fix for varargin and varargout change
John W. Eaton <jwe@octave.org>
parents: 7031
diff changeset
460
6558
e0e1c6df5ab2 [project @ 2007-04-20 19:33:24 by jwe]
jwe
parents: 6556
diff changeset
461 @DOCSTRING(deal)
e0e1c6df5ab2 [project @ 2007-04-20 19:33:24 by jwe]
jwe
parents: 6556
diff changeset
462
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 4029
diff changeset
463 @node Returning From a Function
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
464 @section Returning From a Function
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
465
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
466 The body of a user-defined function can contain a @code{return} statement.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
467 This statement returns control to the rest of the Octave program. It
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
468 looks like this:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
469
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
470 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
471 return
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
472 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
473
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
474 Unlike the @code{return} statement in C, Octave's @code{return}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
475 statement cannot be used to return a value from a function. Instead,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
476 you must assign values to the list of return variables that are part of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
477 the @code{function} statement. The @code{return} statement simply makes
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
478 it easier to exit a function from a deeply nested loop or conditional
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
479 statement.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
480
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
481 Here is an example of a function that checks to see if any elements of a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
482 vector are nonzero.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
483
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
484 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
485 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
486 function retval = any_nonzero (v)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
487 retval = 0;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
488 for i = 1:length (v)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
489 if (v (i) != 0)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
490 retval = 1;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
491 return;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
492 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
493 endfor
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
494 printf ("no nonzero elements found\n");
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
495 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
496 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
497 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
498
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
499 Note that this function could not have been written using the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
500 @code{break} statement to exit the loop once a nonzero value is found
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
501 without adding extra logic to avoid printing the message if the vector
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
502 does contain a nonzero element.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
503
5763
2d055c8fa019 [project @ 2006-04-14 16:54:40 by jwe]
jwe
parents: 5016
diff changeset
504 @deffn {Keyword} return
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
505 When Octave encounters the keyword @code{return} inside a function or
5016
bdbee5282954 [project @ 2004-09-22 02:50:35 by jwe]
jwe
parents: 4933
diff changeset
506 script, it returns control to the caller immediately. At the top level,
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
507 the return statement is ignored. A @code{return} statement is assumed
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
508 at the end of every function definition.
5763
2d055c8fa019 [project @ 2006-04-14 16:54:40 by jwe]
jwe
parents: 5016
diff changeset
509 @end deffn
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
510
3371
86873384cd10 [project @ 1999-11-21 17:31:07 by jwe]
jwe
parents: 3294
diff changeset
511 @DOCSTRING(return_last_computed_value)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
512
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
513 @node Default Arguments
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
514 @section Default Arguments
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
515 @cindex default arguments
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
516
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
517 Since Octave supports variable number of input arguments, it is very useful
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
518 to assign default values to some input arguments. When an input argument
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
519 is declared in the argument list it is possible to assign a default
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
520 value to the argument like this
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
521
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
522 @example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
523 function @var{name} (@var{arg1} = @var{val1}, @dots{})
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
524 @var{body}
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
525 endfunction
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
526 @end example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
527
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
528 @noindent
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
529 If no value is assigned to @var{arg1} by the user, it will have the
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
530 value @var{val1}.
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
531
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
532 As an example, the following function implements a variant of the classic
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
533 ``Hello, World'' program.
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
534 @example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
535 function hello (who = "World")
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
536 printf ("Hello, %s!\n", who);
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
537 endfunction
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
538 @end example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
539
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
540 @noindent
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
541 When called without an input argument the function prints the following
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
542 @example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
543 @group
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
544 hello ();
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
545 @print{} Hello, World!
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
546 @end group
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
547 @end example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
548
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
549 @noindent
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
550 and when it's called with an input argument it prints the following
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
551 @example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
552 @group
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
553 hello ("Beautiful World of Free Software");
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
554 @print{} Hello, Beautiful World of Free Software!
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
555 @end group
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
556 @end example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
557
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
558 Sometimes it is useful to explicitly tell Octave to use the default value
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
559 of an input argument. This can be done writing a @samp{:} as the value
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
560 of the input argument when calling the function.
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
561 @example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
562 @group
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
563 hello (:);
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
564 @print{} Hello, World!
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
565 @end group
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
566 @end example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
567
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 4029
diff changeset
568 @node Function Files
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
569 @section Function Files
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
570 @cindex function file
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
571
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
572 Except for simple one-shot programs, it is not practical to have to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
573 define all the functions you need each time you need them. Instead, you
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
574 will normally want to save them in a file so that you can easily edit
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
575 them, and save them for use at a later time.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
576
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
577 Octave does not require you to load function definitions from files
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
578 before using them. You simply need to put the function definitions in a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
579 place where Octave can find them.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
580
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
581 When Octave encounters an identifier that is undefined, it first looks
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
582 for variables or functions that are already compiled and currently
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
583 listed in its symbol table. If it fails to find a definition there, it
6556
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
584 searches a list of directories (the @dfn{path}) for files ending in
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
585 @file{.m} that have the same base name as the undefined
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
586 identifier.@footnote{The @samp{.m} suffix was chosen for compatibility
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
587 with @sc{Matlab}.} Once Octave finds a file with a name that matches,
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
588 the contents of the file are read. If it defines a @emph{single}
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
589 function, it is compiled and executed. @xref{Script Files}, for more
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
590 information about how you can define more than one function in a single
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
591 file.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
592
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
593 When Octave defines a function from a function file, it saves the full
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
594 name of the file it read and the time stamp on the file. If the time
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
595 stamp on the file changes, Octave may reload the file. When Octave is
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
596 running interactively, time stamp checking normally happens at most once
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
597 each time Octave prints the prompt. Searching for new function
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
598 definitions also occurs if the current working directory changes.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
599
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
600 Checking the time stamp allows you to edit the definition of a function
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
601 while Octave is running, and automatically use the new function
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
602 definition without having to restart your Octave session.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
603
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
604 To avoid degrading performance unnecessarily by checking the time stamps
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
605 on functions that are not likely to change, Octave assumes that function
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
606 files in the directory tree
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
607 @file{@var{octave-home}/share/octave/@var{version}/m}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
608 will not change, so it doesn't have to check their time stamps every time the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
609 functions defined in those files are used. This is normally a very good
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
610 assumption and provides a significant improvement in performance for the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
611 function files that are distributed with Octave.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
612
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
613 If you know that your own function files will not change while you are
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
614 running Octave, you can improve performance by calling
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
615 @code{ignore_function_time_stamp ("all")}, so that Octave will
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
616 ignore the time stamps for all function files. Passing
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
617 @code{"system"} to this function resets the default behavior.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
618
5775
ace8d8d26933 [project @ 2006-04-24 19:13:06 by jwe]
jwe
parents: 5763
diff changeset
619 @c FIXME -- note about time stamps on files in NFS environments?
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
620
6549
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
621 @DOCSTRING(mfilename)
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
622
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
623 @DOCSTRING(ignore_function_time_stamp)
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
624
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
625 @menu
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
626 * Manipulating the load path::
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
627 * Subfunctions::
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
628 * Overloading and Autoloading::
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
629 * Function Locking::
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
630 @end menu
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
631
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
632 @node Manipulating the load path
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
633 @subsection Manipulating the load path
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
634
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
635 When a function is called Octave searches a list of directories for
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
636 a file that contains the function declaration. This list of directories
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
637 is known as the load path. By default the load path contains
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
638 a list of directories distributed with Octave plus the current
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
639 working directory. To see your current load path call the @code{path}
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
640 function without any input or output arguments.
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
641
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
642 It is possible to add or remove directories to or from the load path
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
643 using the @code{addpath} and @code{rmpath}. As an example, the following
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
644 code adds @samp{~/Octave} to the load path.
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
645
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
646 @example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
647 addpath("~/Octave")
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
648 @end example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
649
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
650 @noindent
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
651 After this the directory @samp{~/Octave} will be searched for functions.
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
652
6502
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 6501
diff changeset
653 @DOCSTRING(addpath)
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 6501
diff changeset
654
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 6501
diff changeset
655 @DOCSTRING(genpath)
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 6501
diff changeset
656
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 6501
diff changeset
657 @DOCSTRING(rmpath)
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 6501
diff changeset
658
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 6501
diff changeset
659 @DOCSTRING(savepath)
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 6501
diff changeset
660
6477
a441d6681364 [project @ 2007-03-29 13:44:44 by jwe]
jwe
parents: 5775
diff changeset
661 @DOCSTRING(path)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
662
6502
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 6501
diff changeset
663 @DOCSTRING(pathdef)
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 6501
diff changeset
664
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 6501
diff changeset
665 @DOCSTRING(pathsep)
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 6501
diff changeset
666
3428
5b77cf82393c [project @ 2000-01-14 02:54:53 by jwe]
jwe
parents: 3402
diff changeset
667 @DOCSTRING(rehash)
5b77cf82393c [project @ 2000-01-14 02:54:53 by jwe]
jwe
parents: 3402
diff changeset
668
5b77cf82393c [project @ 2000-01-14 02:54:53 by jwe]
jwe
parents: 3402
diff changeset
669 @DOCSTRING(file_in_loadpath)
5b77cf82393c [project @ 2000-01-14 02:54:53 by jwe]
jwe
parents: 3402
diff changeset
670
7638
2df457529cfa implement expm1 and log1p functions
Jaroslav Hajek <highegg@gmail.com>
parents: 7588
diff changeset
671 @DOCSTRING(restoredefaultpath)
2df457529cfa implement expm1 and log1p functions
Jaroslav Hajek <highegg@gmail.com>
parents: 7588
diff changeset
672
6556
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
673 @node Subfunctions
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
674 @subsection Subfunctions
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
675
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
676 A function file may contain secondary functions called
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
677 @dfn{subfunctions}. These secondary functions are only visible to the
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
678 other functions in the same function file. For example, a file
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
679 @file{f.m} containing
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
680
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
681 @example
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
682 @group
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
683 function f ()
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
684 printf ("in f, calling g\n");
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
685 g ()
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
686 endfunction
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
687 function g ()
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
688 printf ("in g, calling h\n");
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
689 h ()
6556
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
690 endfunction
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
691 function h ()
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
692 printf ("in h\n")
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
693 endfunction
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
694 @end group
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
695 @end example
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
696
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
697 @noindent
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
698 defines a main function @code{f} and two subfunctions. The
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
699 subfunctions @code{g} and @code{h} may only be called from the main
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
700 function @code{f} or from the other subfunctions, but not from outside
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
701 the file @file{f.m}.
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
702
6635
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
703 @node Overloading and Autoloading
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
704 @subsection Overloading and Autoloading
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
705
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
706 The @code{dispatch} function can be used to alias one function name to
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
707 another. It can be used to alias all calls to a particular function name
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
708 to another function, or the alias can be limited to only a particular
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
709 variable type. Consider the example
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
710
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
711 @example
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
712 @group
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
713 function y = spsin (x)
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
714 printf ("Calling spsin\n");
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
715 fflush(stdout);
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
716 y = spfun ("sin", x);
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
717 endfunction
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
718
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
719 dispatch ("sin", "spsin", "sparse matrix");
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
720 y0 = sin(eye(3));
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
721 y1 = sin(speye(3));
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
722 @end group
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
723 @end example
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
724
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
725 @noindent
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
726 Which aliases the @code{spsin} to @code{sin}, but only for real sparse
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
727 matrices. Note that the builtin @code{sin} already correctly treats
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
728 sparse matrices and so this example is only illustrative.
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
729
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
730 @DOCSTRING(dispatch)
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
731
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
732 @DOCSTRING(builtin)
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
733
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
734 A single dynamically linked file might define several
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
735 functions. However, as Octave searches for functions based on the
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
736 functions filename, Octave needs a manner in which to find each of the
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
737 functions in the dynamically linked file. On operating systems that
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
738 support symbolic links, it is possible to create a symbolic link to the
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
739 original file for each of the functions which it contains.
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
740
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
741 However, there is at least one well known operating system that doesn't
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
742 support symbolic links. Making copies of the original file for each of
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
743 the functions is also possible, but is undesirable as it multiples the
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
744 amount of disk space used by Octave. Instead Octave supplies the
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
745 @code{autoload} function, that permits the user to define in which
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
746 file a certain function will be found.
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
747
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
748 @DOCSTRING(autoload)
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
749
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
750 @node Function Locking
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
751 @subsection Function Locking
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
752
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
753 It is sometime desirable to lock a function into memory with the
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
754 @code{mlock} function. This is typically used for dynamically linked
6899
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
755 functions in Oct-files or mex-files that contain some initialization,
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
756 and it is desirable that calling @code{clear} does not remove this
6635
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
757 initialization.
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
758
6899
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
759 As an example,
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
760
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
761 @example
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
762 mlock ("my_function");
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
763 @end example
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
764
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
765 @noindent
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
766 prevents @code{my_function} from being removed from memory, even if
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
767 @code{clear} is called. It is possible to determine if a function is
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
768 locked into memory with the @code{mislocked}, and to unlock a function
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
769 with @code{munlock}, which the following illustrates.
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
770
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
771 @example
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
772 @group
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
773 mlock ("my_function");
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
774 mislocked ("my_function")
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
775 @result{} ans = 1
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
776 munlock ("my_function");
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
777 mislocked ("my_function")
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
778 @result{} ans = 0
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
779 @end group
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
780 @end example
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
781
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
782 A common use of @code{mlock} is to prevent persistent variables from
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
783 being removed from memory, as the following example shows.
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
784
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
785 @example
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
786 @group
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
787 function count_calls()
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
788 persistent calls = 0;
7031
120f3135952f [project @ 2007-10-15 15:30:03 by jwe]
jwe
parents: 7018
diff changeset
789 printf ("'count_calls' has been called %d times\n",
120f3135952f [project @ 2007-10-15 15:30:03 by jwe]
jwe
parents: 7018
diff changeset
790 ++calls);
6899
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
791 endfunction
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
792 mlock ("count_calls");
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
793
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
794 count_calls ();
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
795 @print{} 'count_calls' has been called 1 times
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
796
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
797 clear count_calls
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
798 count_calls ();
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
799 @print{} 'count_calls' has been called 2 times
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
800 @end group
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
801 @end example
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
802
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
803 @noindent
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
804 It is, however, often inconvenient to lock a function from the prompt,
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
805 so it is also possible to lock a function from within its body. This
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
806 is simply done by calling @code{mlock} from within the function.
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
807
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
808 @example
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
809 @group
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
810 function count_calls ()
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
811 mlock ();
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
812 persistent calls = 0;
7031
120f3135952f [project @ 2007-10-15 15:30:03 by jwe]
jwe
parents: 7018
diff changeset
813 printf ("'count_calls' has been called %d times\n",
120f3135952f [project @ 2007-10-15 15:30:03 by jwe]
jwe
parents: 7018
diff changeset
814 ++calls);
6899
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
815 endfunction
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
816 @end group
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
817 @end example
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
818
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
819 @code{mlock} might equally be used to prevent changes to a function from having
6635
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
820 effect in Octave, though a similar effect can be had with the
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
821 @code{ignore_function_time_stamp} function.
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
822
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
823 @DOCSTRING(mlock)
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
824
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
825 @DOCSTRING(munlock)
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
826
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
827 @DOCSTRING(mislocked)
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
828
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 4029
diff changeset
829 @node Script Files
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
830 @section Script Files
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
831
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
832 A script file is a file containing (almost) any sequence of Octave
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
833 commands. It is read and evaluated just as if you had typed each
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
834 command at the Octave prompt, and provides a convenient way to perform a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
835 sequence of commands that do not logically belong inside a function.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
836
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
837 Unlike a function file, a script file must @emph{not} begin with the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
838 keyword @code{function}. If it does, Octave will assume that it is a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
839 function file, and that it defines a single function that should be
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
840 evaluated as soon as it is defined.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
841
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
842 A script file also differs from a function file in that the variables
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
843 named in a script file are not local variables, but are in the same
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
844 scope as the other variables that are visible on the command line.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
845
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
846 Even though a script file may not begin with the @code{function}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
847 keyword, it is possible to define more than one function in a single
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
848 script file and load (but not execute) all of them at once. To do
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
849 this, the first token in the file (ignoring comments and other white
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
850 space) must be something other than @code{function}. If you have no
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
851 other statements to evaluate, you can use a statement that has no
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
852 effect, like this:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
853
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
854 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
855 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
856 # Prevent Octave from thinking that this
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
857 # is a function file:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
858
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
859 1;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
860
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
861 # Define function one:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
862
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
863 function one ()
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
864 ...
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
865 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
866 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
867
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
868 To have Octave read and compile these functions into an internal form,
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
869 you need to make sure that the file is in Octave's load path
6477
a441d6681364 [project @ 2007-03-29 13:44:44 by jwe]
jwe
parents: 5775
diff changeset
870 (accessible through the @code{path} function), then simply type the
a441d6681364 [project @ 2007-03-29 13:44:44 by jwe]
jwe
parents: 5775
diff changeset
871 base name of the file that contains the commands. (Octave uses the
a441d6681364 [project @ 2007-03-29 13:44:44 by jwe]
jwe
parents: 5775
diff changeset
872 same rules to search for script files as it does to search for
a441d6681364 [project @ 2007-03-29 13:44:44 by jwe]
jwe
parents: 5775
diff changeset
873 function files.)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
874
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
875 If the first token in a file (ignoring comments) is @code{function},
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
876 Octave will compile the function and try to execute it, printing a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
877 message warning about any non-whitespace characters that appear after
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
878 the function definition.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
879
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
880 Note that Octave does not try to look up the definition of any identifier
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
881 until it needs to evaluate it. This means that Octave will compile the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
882 following statements if they appear in a script file, or are typed at
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
883 the command line,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
884
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
885 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
886 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
887 # not a function file:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
888 1;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
889 function foo ()
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
890 do_something ();
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
891 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
892 function do_something ()
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
893 do_something_else ();
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
894 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
895 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
896 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
897
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
898 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
899 even though the function @code{do_something} is not defined before it is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
900 referenced in the function @code{foo}. This is not an error because
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
901 Octave does not need to resolve all symbols that are referenced by a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
902 function until the function is actually evaluated.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
903
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
904 Since Octave doesn't look for definitions until they are needed, the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
905 following code will always print @samp{bar = 3} whether it is typed
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
906 directly on the command line, read from a script file, or is part of a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
907 function body, even if there is a function or script file called
6477
a441d6681364 [project @ 2007-03-29 13:44:44 by jwe]
jwe
parents: 5775
diff changeset
908 @file{bar.m} in Octave's path.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
909
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
910 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
911 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
912 eval ("bar = 3");
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
913 bar
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
914 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
915 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
916
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
917 Code like this appearing within a function body could fool Octave if
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
918 definitions were resolved as the function was being compiled. It would
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
919 be virtually impossible to make Octave clever enough to evaluate this
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
920 code in a consistent fashion. The parser would have to be able to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
921 perform the call to @code{eval} at compile time, and that would be
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
922 impossible unless all the references in the string to be evaluated could
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
923 also be resolved, and requiring that would be too restrictive (the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
924 string might come from user input, or depend on things that are not
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
925 known until the function is evaluated).
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
926
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
927 Although Octave normally executes commands from script files that have
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
928 the name @file{@var{file}.m}, you can use the function @code{source} to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
929 execute commands from any file.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
930
3371
86873384cd10 [project @ 1999-11-21 17:31:07 by jwe]
jwe
parents: 3294
diff changeset
931 @DOCSTRING(source)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
932
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
933 @node Function Handles Inline Functions and Anonymous Functions
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
934 @section Function Handles, Inline Functions, and Anonymous Functions
4933
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
935 @cindex handle, function handles
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
936 @cindex inline, inline functions
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
937 @cindex anonymous functions
4933
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
938
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
939 It can be very convenient store a function in a variable so that it
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
940 can be passed to a different function. For example, a function that
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
941 performs numerical minimisation needs access to the function that
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
942 should be minimised.
4933
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
943
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
944 @menu
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
945 * Function Handles::
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
946 * Anonymous Functions::
4933
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
947 * Inline Functions::
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
948 @end menu
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
949
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
950 @node Function Handles
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
951 @subsection Function Handles
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
952
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
953 A function handle is a pointer to another function and is defined with
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
954 the syntax
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
955
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
956 @example
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
957 @@@var{function-name}
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
958 @end example
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
959
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
960 @noindent
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
961 For example
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
962
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
963 @example
6556
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
964 f = @@sin;
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
965 @end example
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
966
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
967 @noindent
6570
49f0820425a8 [project @ 2007-04-24 23:06:56 by jwe]
jwe
parents: 6558
diff changeset
968 Creates a function handle called @code{f} that refers to the
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
969 function @code{sin}.
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
970
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
971 Function handles are used to call other functions indirectly, or to pass
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
972 a function as an argument to another function like @code{quad} or
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
973 @code{fsolve}. For example
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
974
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
975 @example
6556
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
976 f = @@sin;
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
977 quad (f, 0, pi)
6929
d5bf6be68c87 [project @ 2007-09-28 09:21:40 by dbateman]
dbateman
parents: 6899
diff changeset
978 @result{} 2
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
979 @end example
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
980
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
981 You may use @code{feval} to call a function using function handle, or
6570
49f0820425a8 [project @ 2007-04-24 23:06:56 by jwe]
jwe
parents: 6558
diff changeset
982 simply write the name of the function handle followed by an argument
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
983 list. If there are no arguments, you must use an empty argument list
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
984 @samp{()}. For example
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
985
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
986 @example
6556
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
987 f = @@sin;
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
988 feval (f, pi/4)
6570
49f0820425a8 [project @ 2007-04-24 23:06:56 by jwe]
jwe
parents: 6558
diff changeset
989 @result{} 0.70711
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
990 f (pi/4)
6570
49f0820425a8 [project @ 2007-04-24 23:06:56 by jwe]
jwe
parents: 6558
diff changeset
991 @result{} 0.70711
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
992 @end example
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
993
4933
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
994 @DOCSTRING(functions)
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
995
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
996 @DOCSTRING(func2str)
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
997
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
998 @DOCSTRING(str2func)
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
999
6570
49f0820425a8 [project @ 2007-04-24 23:06:56 by jwe]
jwe
parents: 6558
diff changeset
1000 @node Anonymous Functions
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1001 @subsection Anonymous Functions
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1002
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1003 Anonymous functions are defined using the syntax
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1004
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1005 @example
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1006 @@(@var{argument-list}) @var{expression}
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1007 @end example
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1008
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1009 @noindent
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1010 Any variables that are not found in the argument list are inherited from
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1011 the enclosing scope. Anonymous functions are useful for creating simple
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1012 unnamed functions from expressions or for wrapping calls to other
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1013 functions to adapt them for use by functions like @code{quad}. For
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1014 example,
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1015
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1016 @example
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1017 f = @@(x) x.^2;
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1018 quad (f, 0, 10)
6570
49f0820425a8 [project @ 2007-04-24 23:06:56 by jwe]
jwe
parents: 6558
diff changeset
1019 @result{} 333.33
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1020 @end example
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1021
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1022 @noindent
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1023 creates a simple unnamed function from the expression @code{x.^2} and
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1024 passes it to @code{quad},
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1025
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1026 @example
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1027 quad (@@(x) sin (x), 0, pi)
6933
d09b8a5d09e2 [project @ 2007-09-28 10:59:51 by dbateman]
dbateman
parents: 6929
diff changeset
1028 @result{} 2
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1029 @end example
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1030
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1031 @noindent
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1032 wraps another function, and
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1033
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1034 @example
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1035 a = 1;
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1036 b = 2;
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1037 quad (@@(x) betainc (x, a, b), 0, 0.4)
6929
d5bf6be68c87 [project @ 2007-09-28 09:21:40 by dbateman]
dbateman
parents: 6899
diff changeset
1038 @result{} 0.13867
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1039 @end example
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1040
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1041 @noindent
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1042 adapts a function with several parameters to the form required by
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1043 @code{quad}. In this example, the values of @var{a} and @var{b} that
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1044 are passed to @code{betainc} are inherited from the current
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1045 environment.
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1046
4933
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1047 @node Inline Functions
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1048 @subsection Inline Functions
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1049
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1050 An inline function is created from a string containing the function
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1051 body using the @code{inline} function. The following code defines the
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1052 function @math{f(x) = x^2 + 2}.
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1053
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1054 @example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1055 f = inline("x^2 + 2");
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1056 @end example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1057
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1058 @noindent
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1059 After this it is possible to evaluate @math{f} at any @math{x} by
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1060 writing @code{f(x)}.
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1061
4933
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1062 @DOCSTRING(inline)
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1063
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1064 @DOCSTRING(argnames)
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1065
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1066 @DOCSTRING(formula)
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1067
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1068 @DOCSTRING(vectorize)
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1069
7984
bbaa5d7d0143 Some documentation updates
David Bateman <dbateman@free.fr>
parents: 7638
diff changeset
1070 @DOCSTRING(symvar)
bbaa5d7d0143 Some documentation updates
David Bateman <dbateman@free.fr>
parents: 7638
diff changeset
1071
6549
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
1072 @node Commands
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
1073 @section Commands
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
1074
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1075 Commands are a special class of functions that only accept string
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1076 input arguments. A command can be called as an ordinary function, but
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1077 it can also be called without the parentheses like the following example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1078 shows
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1079
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1080 @example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1081 my_command hello world
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1082 @end example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1083
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1084 @noindent
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1085 which is the same as
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1086
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1087 @example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1088 my_command("hello", "world")
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1089 @end example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1090
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1091 The general form of a command call is
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1092
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1093 @example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1094 @var{name} @var{arg1} @var{arg2} @dots{}
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1095 @end example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1096
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1097 @noindent
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1098 which translates directly to
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1099
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1100 @example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1101 @var{name} ("@var{arg1}", "@var{arg2}", @dots{})
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1102 @end example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1103
7001
8b0cfeb06365 [project @ 2007-10-10 18:02:59 by jwe]
jwe
parents: 6933
diff changeset
1104 A function can be used as a command if it accepts string input arguments.
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1105 To do this, the function must be marked as a command, which can be done
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1106 with the @code{mark_as_command} command like this
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1107
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1108 @example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1109 mark_as_command name
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1110 @end example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1111
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1112 @noindent
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1113 where @code{name} is the function to be marked as a command.
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1114
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1115 One difficulty of commands occurs when one of the string input arguments
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1116 are stored in a variable. Since Octave can't tell the difference between
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1117 a variable name, and an ordinary string, it is not possible to pass a
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1118 variable as input to a command. In such a situation a command must be
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1119 called as a function.
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1120
6549
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
1121 @DOCSTRING(mark_as_command)
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
1122
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
1123 @DOCSTRING(unmark_command)
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
1124
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
1125 @DOCSTRING(iscommand)
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
1126
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
1127 @DOCSTRING(mark_as_rawcommand)
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
1128
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
1129 @DOCSTRING(unmark_rawcommand)
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
1130
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
1131 @DOCSTRING(israwcommand)
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
1132
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 4029
diff changeset
1133 @node Organization of Functions
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1134 @section Organization of Functions Distributed with Octave
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1135
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1136 Many of Octave's standard functions are distributed as function files.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1137 They are loosely organized by topic, in subdirectories of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1138 @file{@var{octave-home}/lib/octave/@var{version}/m}, to make it easier
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1139 to find them.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1140
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1141 The following is a list of all the function file subdirectories, and the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1142 types of functions you will find there.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1143
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1144 @table @file
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1145 @item audio
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1146 Functions for playing and recording sounds.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1147
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1148 @item control
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1149 Functions for design and simulation of automatic control systems.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1150
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1151 @item elfun
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1152 Elementary functions.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1153
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1154 @item finance
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1155 Functions for computing interest payments, investment values, and rates
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1156 of return.
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1157
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1158 @item general
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1159 Miscellaneous matrix manipulations, like @code{flipud}, @code{rot90},
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1160 and @code{triu}, as well as other basic functions, like
4029
2cc57b6169cf [project @ 2002-08-09 07:36:15 by jwe]
jwe
parents: 3760
diff changeset
1161 @code{ismatrix}, @code{nargchk}, etc.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1162
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1163 @item image
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1164 Image processing tools. These functions require the X Window System.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1165
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1166 @item io
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1167 Input-ouput functions.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1168
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1169 @item linear-algebra
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1170 Functions for linear algebra.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1171
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1172 @item miscellaneous
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1173 Functions that don't really belong anywhere else.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1174
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1175 @item optimization
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1176 Minimization of functions.
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1177
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1178 @item path
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1179 Functions to manage the directory path Octave uses to find functions.
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1180
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1181 @item pkg
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1182 Install external packages of functions in Octave.
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1183
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1184 @item plot
6556
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
1185 Functions for displaying and printing two- and three-dimensional graphs.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1186
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1187 @item polynomial
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1188 Functions for manipulating polynomials.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1189
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1190 @item set
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1191 Functions for creating and manipulating sets of unique values.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1192
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1193 @item signal
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1194 Functions for signal processing applications.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1195
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1196 @item sparse
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1197 Functions for handling sparse matrices.
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1198
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1199 @item specfun
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1200 Special functions.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1201
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1202 @item special-matrix
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1203 Functions that create special matrix forms.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1204
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1205 @item startup
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1206 Octave's system-wide startup file.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1207
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1208 @item statistics
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1209 Statistical functions.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1210
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1211 @item strings
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1212 Miscellaneous string-handling functions.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1213
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1214 @item testfun
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1215 Perform unit tests on other functions.
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1216
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1217 @item time
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1218 Functions related to time keeping.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1219 @end table