annotate doc/interpreter/func.txi @ 14116:951eacaf9381 stable

Initial documentation for broadcasting and general vectorization guidelines * vectorize.txi: New file. * NEWS: Update with location of broadcasting documentation. * Makefile.am: Add vectorize.texi * arith.txi: Move accumarray and accumdim docstring to vectorize.txi * container.txi: Move structfun docstring to vectorize.txi * expr.txi: Mention broadcasting where relevant. * func.txi: Move vectorize docstring to vectorize.txi * matrix.txi: Move function application section to vectorize.txi * octave.texi: Add vectorize.txi and its menu options * sparse.txi: Move spfun to vectorize.txi * tips.txi: Move and rewrite coding tips section in vectorize.txi * bsxfun.h (is_valid_bsxfun, is_valid_inplace_bsxfun): Rename warning to "Octave:broadcast" * accumdim.m: Reformat to use @example in lieu of @smallexample * warning_ids.m: Add Octave:broadcast * bsxfun.cc: Reword docstring to mention broadcasting * cellfun.cc: Move comment about efficiency from tips.txi * version.h.in: Add a big startup warning about broadcasting
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Tue, 27 Dec 2011 15:15:41 -0500
parents b0cdd60db5e5
children b25559bd5aaa
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
1 @c Copyright (C) 1996-2011 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
9032
349616d9c38e Cleanup top-level documentation menu in octave.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
20 @chapter Functions and Scripts
3294
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::
10209
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9209
diff changeset
35 * Ignoring Arguments::
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
36 * Variable-length Return Lists::
11255
d682cd6669ac Update info-based documentation menus to include new nodes.
Rik <octave@nomad.inbox5.com>
parents: 10828
diff changeset
37 * Returning from a Function::
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
38 * Default Arguments::
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
39 * Function Files::
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
40 * Script Files::
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
41 * Function Handles Inline Functions and Anonymous Functions::
6549
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
42 * Commands::
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
43 * Organization of Functions::
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
44 @end menu
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
45
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 4029
diff changeset
46 @node Defining Functions
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
47 @section Defining Functions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
48 @cindex @code{function} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
49 @cindex @code{endfunction} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
50
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
51 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
52 looks like this:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
53
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
54 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
55 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
56 function @var{name}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
57 @var{body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
58 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
59 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
60 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
61
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
62 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
63 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
64 letters, digits and underscores, not starting with a digit. Functions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
65 share the same pool of names as variables.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
66
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
67 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
68 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
69 should actually @emph{do}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
70
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
71 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
72 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
73
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
74 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
75 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
76 function wakeup
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
77 printf ("\a");
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
78 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
79 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
80 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
81
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
82 The @code{printf} statement (@pxref{Input and Output}) simply tells
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
83 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
84 stands for the alert character (ASCII 7). @xref{Strings}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
85
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
86 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
87 typing the name of the function.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
88
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
89 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
90 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
91
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
92 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
93 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
94 function @var{name} (@var{arg-list})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
95 @var{body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
96 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
97 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
98 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
99
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
100 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
101 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
102 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
103 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
104 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
105
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
106 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
107 @code{wakeup} to look like this:
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
108
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
109 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
110 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
111 function wakeup (message)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
112 printf ("\a%s\n", message);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
113 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
114 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
115 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
116
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
117 Calling this function using a statement like this
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
118
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
119 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
120 wakeup ("Rise and shine!");
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
121 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
122
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
123 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
124 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
125 @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
126 in the first argument to the @code{printf} statement).
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
127
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
128 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
129 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
130 returns a single value:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
131
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
132 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
133 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
134 function @var{ret-var} = @var{name} (@var{arg-list})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
135 @var{body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
136 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
137 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
138 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
139
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
140 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
141 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
142 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
143 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
144 a value.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
145
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
146 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
147 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
148 local to the function. @xref{Global Variables}, for information about
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
149 how to access global variables inside a function.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
150
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
151 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
152 elements of a vector:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
153
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
154 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
155 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
156 function retval = avg (v)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
157 retval = sum (v) / length (v);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
158 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
159 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
160 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
161
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
162 If we had written @code{avg} like this instead,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
163
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
164 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
165 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
166 function retval = avg (v)
4029
2cc57b6169cf [project @ 2002-08-09 07:36:15 by jwe]
jwe
parents: 3760
diff changeset
167 if (isvector (v))
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
168 retval = sum (v) / length (v);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
169 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
170 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
171 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
172 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
173
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
174 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
175 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
176 argument, Octave would have printed an error message like this:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
177
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
178 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
179 @group
8015
30629059b72d Update the manual to reflect the changes in error output
sh@sh-laptop
parents: 7984
diff changeset
180 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
181 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
182 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
183
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
184 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
185 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
186 @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
187 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
188 always have values, and to produce meaningful error messages when
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
189 problems are encountered. For example, @code{avg} could have been
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
190 written like this:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
191
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
192 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
193 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
194 function retval = avg (v)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
195 retval = 0;
4029
2cc57b6169cf [project @ 2002-08-09 07:36:15 by jwe]
jwe
parents: 3760
diff changeset
196 if (isvector (v))
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
197 retval = sum (v) / length (v);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
198 else
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
199 error ("avg: expecting vector argument");
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
200 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
201 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
202 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
203 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
204
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
205 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
206 called without an argument? Without additional error checking, Octave
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
207 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
208 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
209 Octave provides each function with an automatic variable called
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
210 @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
211 automatically initialized to the number of arguments that have actually
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
212 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
213 @code{avg} function like this:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
214
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
215 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
216 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
217 function retval = avg (v)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
218 retval = 0;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
219 if (nargin != 1)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
220 usage ("avg (vector)");
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
221 endif
4029
2cc57b6169cf [project @ 2002-08-09 07:36:15 by jwe]
jwe
parents: 3760
diff changeset
222 if (isvector (v))
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
223 retval = sum (v) / length (v);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
224 else
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
225 error ("avg: expecting vector argument");
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
226 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
227 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
228 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
229 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
230
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
231 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
232 function with more arguments than expected, doing so probably indicates
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
233 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
234 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
235 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
236 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
237 possibilities and issue our own error message.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
238
4700
ca3a1d687bba [project @ 2004-01-21 03:28:31 by jwe]
jwe
parents: 4466
diff changeset
239 @DOCSTRING(nargin)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
240
6558
e0e1c6df5ab2 [project @ 2007-04-20 19:33:24 by jwe]
jwe
parents: 6556
diff changeset
241 @DOCSTRING(inputname)
e0e1c6df5ab2 [project @ 2007-04-20 19:33:24 by jwe]
jwe
parents: 6556
diff changeset
242
3371
86873384cd10 [project @ 1999-11-21 17:31:07 by jwe]
jwe
parents: 3294
diff changeset
243 @DOCSTRING(silent_functions)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
244
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 4029
diff changeset
245 @node Multiple Return Values
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
246 @section Multiple Return Values
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
247
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
248 Unlike many other computer languages, Octave allows you to define
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
249 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
250 functions that return multiple values is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
251
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
252 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
253 @group
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
254 function [@var{ret-list}] = @var{name} (@var{arg-list})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
255 @var{body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
256 endfunction
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
257 @end group
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
258 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
259
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
260 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
261 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
262 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
263 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
264 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
265 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
266 equivalent to the form described in the previous section.
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 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
269 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
270
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
271 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
272 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
273 function [max, idx] = vmax (v)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
274 idx = 1;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
275 max = v (idx);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
276 for i = 2:length (v)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
277 if (v (i) > max)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
278 max = v (i);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
279 idx = i;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
280 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
281 endfor
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
282 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
283 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
284 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
285
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
286 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
287 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
288 convenient. The values to be returned may not have compatible
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
289 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
290 values distinct names.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
291
13749
62d1f56b0be7 New nthargout function
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12642
diff changeset
292 It is possible to use the @code{nthargout} function to obtain only some
14038
b0cdd60db5e5 doc: Grammarcheck documentation ahead of 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 13852
diff changeset
293 of the return values or several at once in a cell array.
b0cdd60db5e5 doc: Grammarcheck documentation ahead of 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 13852
diff changeset
294 @ref{Cell Array Objects}
13749
62d1f56b0be7 New nthargout function
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12642
diff changeset
295
62d1f56b0be7 New nthargout function
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12642
diff changeset
296 @DOCSTRING(nthargout)
62d1f56b0be7 New nthargout function
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12642
diff changeset
297
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
298 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
299 Octave also automatically initializes @code{nargout} to the number of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
300 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
301 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
302 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
303 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
304 arguments, so the value of @code{nargout} may be zero.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
305
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
306 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
307 functions that behave differently depending on the value of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
308 @code{nargout}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
309
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
310 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
311 example, calling the function
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
312
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
313 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
314 @group
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
315 function [x, y, z] = f ()
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
316 x = 1;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
317 z = 2;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
318 endfunction
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
319 @end group
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
320 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
321
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
322 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
323 as
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
324
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
325 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
326 [a, b, c] = f ()
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
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
330 produces:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
331
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
332 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
333 @group
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
334 a = 1
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
335
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
336 b = [](0x0)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
337
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
338 c = 2
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
339 @end group
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
340 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
341
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
342 @noindent
6501
68f3125f6e27 [project @ 2007-04-05 20:53:40 by jwe]
jwe
parents: 6477
diff changeset
343 along with a warning.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
344
4700
ca3a1d687bba [project @ 2004-01-21 03:28:31 by jwe]
jwe
parents: 4466
diff changeset
345 @DOCSTRING(nargout)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
346
12629
8c25df502846 func.txi: document use of nargin checking at head of function.
Rik <octave@nomad.inbox5.com>
parents: 12592
diff changeset
347 It is good practice at the head of a function to verify that it has been called
8c25df502846 func.txi: document use of nargin checking at head of function.
Rik <octave@nomad.inbox5.com>
parents: 12592
diff changeset
348 correctly. In Octave the following idiom is seen frequently
8c25df502846 func.txi: document use of nargin checking at head of function.
Rik <octave@nomad.inbox5.com>
parents: 12592
diff changeset
349
8c25df502846 func.txi: document use of nargin checking at head of function.
Rik <octave@nomad.inbox5.com>
parents: 12592
diff changeset
350 @example
8c25df502846 func.txi: document use of nargin checking at head of function.
Rik <octave@nomad.inbox5.com>
parents: 12592
diff changeset
351 @group
8c25df502846 func.txi: document use of nargin checking at head of function.
Rik <octave@nomad.inbox5.com>
parents: 12592
diff changeset
352 if (nargin < min_#_inputs || nargin > max_#_inputs)
8c25df502846 func.txi: document use of nargin checking at head of function.
Rik <octave@nomad.inbox5.com>
parents: 12592
diff changeset
353 print_usage ();
8c25df502846 func.txi: document use of nargin checking at head of function.
Rik <octave@nomad.inbox5.com>
parents: 12592
diff changeset
354 endif
8c25df502846 func.txi: document use of nargin checking at head of function.
Rik <octave@nomad.inbox5.com>
parents: 12592
diff changeset
355 @end group
8c25df502846 func.txi: document use of nargin checking at head of function.
Rik <octave@nomad.inbox5.com>
parents: 12592
diff changeset
356 @end example
8c25df502846 func.txi: document use of nargin checking at head of function.
Rik <octave@nomad.inbox5.com>
parents: 12592
diff changeset
357
8c25df502846 func.txi: document use of nargin checking at head of function.
Rik <octave@nomad.inbox5.com>
parents: 12592
diff changeset
358 @noindent
8c25df502846 func.txi: document use of nargin checking at head of function.
Rik <octave@nomad.inbox5.com>
parents: 12592
diff changeset
359 which stops the function execution and prints a message about the correct
8c25df502846 func.txi: document use of nargin checking at head of function.
Rik <octave@nomad.inbox5.com>
parents: 12592
diff changeset
360 way to call the function whenever the number of inputs is wrong.
8c25df502846 func.txi: document use of nargin checking at head of function.
Rik <octave@nomad.inbox5.com>
parents: 12592
diff changeset
361
13852
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents: 13749
diff changeset
362 For compatibility with @sc{matlab}, @code{nargchk}, @code{narginchk} and
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents: 13749
diff changeset
363 @code{nargoutchk} are available which provide similar error checking.
12629
8c25df502846 func.txi: document use of nargin checking at head of function.
Rik <octave@nomad.inbox5.com>
parents: 12592
diff changeset
364
3371
86873384cd10 [project @ 1999-11-21 17:31:07 by jwe]
jwe
parents: 3294
diff changeset
365 @DOCSTRING(nargchk)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
366
13852
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents: 13749
diff changeset
367 @DOCSTRING(narginchk)
e36817e2ee60 New function narginchk
Carnë Draug <carandraug+dev@gmail.com>
parents: 13749
diff changeset
368
7984
bbaa5d7d0143 Some documentation updates
David Bateman <dbateman@free.fr>
parents: 7638
diff changeset
369 @DOCSTRING(nargoutchk)
bbaa5d7d0143 Some documentation updates
David Bateman <dbateman@free.fr>
parents: 7638
diff changeset
370
8286
6f2d95255911 fix @seealso references to point to existing anchors
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8276
diff changeset
371 @anchor{doc-varargin} @anchor{doc-varargout}
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 4029
diff changeset
372 @node Variable-length Argument Lists
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
373 @section Variable-length Argument Lists
4933
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
374 @cindex variable-length argument lists
8567
674d00f5e072 remove variable index
Soren Hauberg <soren@hauberg.org>
parents: 8347
diff changeset
375 @cindex @code{varargin}
8072
3b6e7d04b614 doc fixes
John W. Eaton <jwe@octave.org>
parents: 8015
diff changeset
376
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
377 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
378 is defined. As an example think of a function that returns the smallest
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
379 of all its input arguments. For example:
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
380
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
381 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
382 @group
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
383 a = smallest (1, 2, 3);
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
384 b = smallest (1, 2, 3, 4);
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
385 @end group
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
386 @end example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
387
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
388 @noindent
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
389 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
390 the @code{smallest} function is
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
391
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
392 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
393 @group
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
394 function val = smallest (arg1, arg2, arg3, arg4, arg5)
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
395 @var{body}
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
396 endfunction
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
397 @end group
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
398 @end example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
399
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
400 @noindent
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
401 and then use the value of @code{nargin} to determine which of the input
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
402 arguments should be considered. The problem with this approach is
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
403 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
404
7588
cbedf652a752 doc fix for varargin and varargout change
John W. Eaton <jwe@octave.org>
parents: 7031
diff changeset
405 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
406 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
407 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
408 looks like this
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
409
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
410 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
411 @group
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
412 function val = smallest (varargin)
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
413 @var{body}
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
414 endfunction
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
415 @end group
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
416 @end example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
417
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
418 @noindent
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
419 In the function body the input arguments can be accessed through the
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
420 variable @code{varargin}. This variable is a cell array containing
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
421 all the input arguments. @xref{Cell Arrays}, for details on working
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
422 with cell arrays. The @code{smallest} function can now be defined
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
423 like this
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
424
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
425 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
426 @group
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
427 function val = smallest (varargin)
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
428 val = min ([varargin@{:@}]);
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
429 endfunction
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
430 @end group
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
431 @end example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
432
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
433 @noindent
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
434 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
435 a very simple solution to the problem.
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 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
438 @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
439 can be defined like this
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
440
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
441 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
442 @group
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
443 function print_arguments (varargin)
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
444 for i = 1:length (varargin)
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
445 printf ("Input argument %d: ", i);
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
446 disp (varargin@{i@});
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
447 endfor
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
448 endfunction
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
449 @end group
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
450 @end example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
451
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
452 @noindent
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
453 This function produces output like this
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
454
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
455 @example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
456 @group
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
457 print_arguments (1, "two", 3);
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
458 @print{} Input argument 1: 1
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
459 @print{} Input argument 2: two
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
460 @print{} Input argument 3: 3
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
461 @end group
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
462 @end example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
463
6558
e0e1c6df5ab2 [project @ 2007-04-20 19:33:24 by jwe]
jwe
parents: 6556
diff changeset
464 @DOCSTRING(parseparams)
e0e1c6df5ab2 [project @ 2007-04-20 19:33:24 by jwe]
jwe
parents: 6556
diff changeset
465
10209
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9209
diff changeset
466 @node Ignoring Arguments
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9209
diff changeset
467 @section Ignoring Arguments
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9209
diff changeset
468
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
469 In the formal argument list, it is possible to use the dummy placeholder
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
470 @code{~} instead of a name. This indicates that the corresponding argument
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
471 value should be ignored and not stored to any variable.
10209
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9209
diff changeset
472
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9209
diff changeset
473 @example
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9209
diff changeset
474 @group
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9209
diff changeset
475 function val = pick2nd (~, arg2)
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9209
diff changeset
476 val = arg2;
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9209
diff changeset
477 endfunction
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9209
diff changeset
478 @end group
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9209
diff changeset
479 @end example
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9209
diff changeset
480
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9209
diff changeset
481 The value of @code{nargin} is not affected by using this declaration.
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9209
diff changeset
482
12592
732a568bf694 Modify func.txi discussion of output arguments to include discussion of isargout.
Rik <octave@nomad.inbox5.com>
parents: 11573
diff changeset
483 Return arguments can also be ignored using the same syntax. Functions may
732a568bf694 Modify func.txi discussion of output arguments to include discussion of isargout.
Rik <octave@nomad.inbox5.com>
parents: 11573
diff changeset
484 take advantage of ignored outputs to reduce the number of calculations
732a568bf694 Modify func.txi discussion of output arguments to include discussion of isargout.
Rik <octave@nomad.inbox5.com>
parents: 11573
diff changeset
485 performed. To do so, use the @code{isargout} function to query whether the
732a568bf694 Modify func.txi discussion of output arguments to include discussion of isargout.
Rik <octave@nomad.inbox5.com>
parents: 11573
diff changeset
486 output argument is wanted. For example:
732a568bf694 Modify func.txi discussion of output arguments to include discussion of isargout.
Rik <octave@nomad.inbox5.com>
parents: 11573
diff changeset
487
732a568bf694 Modify func.txi discussion of output arguments to include discussion of isargout.
Rik <octave@nomad.inbox5.com>
parents: 11573
diff changeset
488 @example
732a568bf694 Modify func.txi discussion of output arguments to include discussion of isargout.
Rik <octave@nomad.inbox5.com>
parents: 11573
diff changeset
489 @group
732a568bf694 Modify func.txi discussion of output arguments to include discussion of isargout.
Rik <octave@nomad.inbox5.com>
parents: 11573
diff changeset
490 function [out1, out2] = long_function (x, y, z)
732a568bf694 Modify func.txi discussion of output arguments to include discussion of isargout.
Rik <octave@nomad.inbox5.com>
parents: 11573
diff changeset
491 if (isargout (1))
732a568bf694 Modify func.txi discussion of output arguments to include discussion of isargout.
Rik <octave@nomad.inbox5.com>
parents: 11573
diff changeset
492 ## Long calculation
732a568bf694 Modify func.txi discussion of output arguments to include discussion of isargout.
Rik <octave@nomad.inbox5.com>
parents: 11573
diff changeset
493 @dots{}
732a568bf694 Modify func.txi discussion of output arguments to include discussion of isargout.
Rik <octave@nomad.inbox5.com>
parents: 11573
diff changeset
494 out1 = result;
732a568bf694 Modify func.txi discussion of output arguments to include discussion of isargout.
Rik <octave@nomad.inbox5.com>
parents: 11573
diff changeset
495 endif
732a568bf694 Modify func.txi discussion of output arguments to include discussion of isargout.
Rik <octave@nomad.inbox5.com>
parents: 11573
diff changeset
496 @dots{}
732a568bf694 Modify func.txi discussion of output arguments to include discussion of isargout.
Rik <octave@nomad.inbox5.com>
parents: 11573
diff changeset
497 endfunction
732a568bf694 Modify func.txi discussion of output arguments to include discussion of isargout.
Rik <octave@nomad.inbox5.com>
parents: 11573
diff changeset
498 @end group
732a568bf694 Modify func.txi discussion of output arguments to include discussion of isargout.
Rik <octave@nomad.inbox5.com>
parents: 11573
diff changeset
499 @end example
732a568bf694 Modify func.txi discussion of output arguments to include discussion of isargout.
Rik <octave@nomad.inbox5.com>
parents: 11573
diff changeset
500
732a568bf694 Modify func.txi discussion of output arguments to include discussion of isargout.
Rik <octave@nomad.inbox5.com>
parents: 11573
diff changeset
501 @DOCSTRING(isargout)
732a568bf694 Modify func.txi discussion of output arguments to include discussion of isargout.
Rik <octave@nomad.inbox5.com>
parents: 11573
diff changeset
502
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 4029
diff changeset
503 @node Variable-length Return Lists
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
504 @section Variable-length Return Lists
4933
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
505 @cindex variable-length return lists
8567
674d00f5e072 remove variable index
Soren Hauberg <soren@hauberg.org>
parents: 8347
diff changeset
506 @cindex @code{varargout}
8072
3b6e7d04b614 doc fixes
John W. Eaton <jwe@octave.org>
parents: 8015
diff changeset
507
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
508 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
509 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
510 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
511 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
512 @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
513 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
514
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
515 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
516 1, the second to 2, and so on.
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
517
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
518 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
519 @group
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
520 function varargout = one_to_n ()
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
521 for i = 1:nargout
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
522 varargout@{i@} = i;
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
523 endfor
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
524 endfunction
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
525 @end group
6510
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 When called this function returns values like this
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
530
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
531 @example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
532 @group
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
533 [a, b, c] = one_to_n ()
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
534 @result{} a = 1
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
535 @result{} b = 2
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
536 @result{} c = 3
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
537 @end group
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
538 @end example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
539
7588
cbedf652a752 doc fix for varargin and varargout change
John W. Eaton <jwe@octave.org>
parents: 7031
diff changeset
540 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
541 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
542 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
543
6558
e0e1c6df5ab2 [project @ 2007-04-20 19:33:24 by jwe]
jwe
parents: 6556
diff changeset
544 @DOCSTRING(deal)
e0e1c6df5ab2 [project @ 2007-04-20 19:33:24 by jwe]
jwe
parents: 6556
diff changeset
545
11255
d682cd6669ac Update info-based documentation menus to include new nodes.
Rik <octave@nomad.inbox5.com>
parents: 10828
diff changeset
546 @node Returning from a Function
d682cd6669ac Update info-based documentation menus to include new nodes.
Rik <octave@nomad.inbox5.com>
parents: 10828
diff changeset
547 @section Returning from a Function
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
548
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
549 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
550 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
551 looks like this:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
552
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
553 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
554 return
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
555 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
556
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
557 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
558 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
559 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
560 the @code{function} statement. The @code{return} statement simply makes
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
561 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
562 statement.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
563
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
564 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
565 vector are nonzero.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
566
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
567 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
568 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
569 function retval = any_nonzero (v)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
570 retval = 0;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
571 for i = 1:length (v)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
572 if (v (i) != 0)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
573 retval = 1;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
574 return;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
575 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
576 endfor
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
577 printf ("no nonzero elements found\n");
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
578 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
579 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
580 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
581
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
582 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
583 @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
584 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
585 does contain a nonzero element.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
586
11547
e1851653d59c Eliminate @deffn macros.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
587 @deftypefn {Keyword} {} return
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
588 When Octave encounters the keyword @code{return} inside a function or
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
589 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
590 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
591 at the end of every function definition.
11547
e1851653d59c Eliminate @deffn macros.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
592 @end deftypefn
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
593
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
594 @node Default Arguments
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
595 @section Default Arguments
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
596 @cindex default arguments
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
597
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
598 Since Octave supports variable number of input arguments, it is very useful
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
599 to assign default values to some input arguments. When an input argument
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
600 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
601 value to the argument like this
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
602
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
603 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
604 @group
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
605 function @var{name} (@var{arg1} = @var{val1}, @dots{})
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
606 @var{body}
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
607 endfunction
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
608 @end group
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
609 @end example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
610
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
611 @noindent
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
612 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
613 value @var{val1}.
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
614
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
615 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
616 ``Hello, World'' program.
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
617
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
618 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
619 @group
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
620 function hello (who = "World")
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
621 printf ("Hello, %s!\n", who);
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
622 endfunction
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
623 @end group
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
624 @end example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
625
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
626 @noindent
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
627 When called without an input argument the function prints the following
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
628
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
629 @example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
630 @group
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
631 hello ();
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
632 @print{} Hello, World!
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
633 @end group
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
634 @end example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
635
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
636 @noindent
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
637 and when it's called with an input argument it prints the following
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
638
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
639 @example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
640 @group
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
641 hello ("Beautiful World of Free Software");
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
642 @print{} Hello, Beautiful World of Free Software!
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
643 @end group
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
644 @end example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
645
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
646 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
647 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
648 of the input argument when calling the function.
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
649
6510
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
650 @example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
651 @group
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
652 hello (:);
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
653 @print{} Hello, World!
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
654 @end group
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
655 @end example
cbae86745c5b [project @ 2007-04-09 23:15:47 by jwe]
jwe
parents: 6502
diff changeset
656
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 4029
diff changeset
657 @node Function Files
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
658 @section Function Files
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
659 @cindex function file
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
660
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
661 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
662 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
663 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
664 them, and save them for use at a later time.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
665
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
666 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
667 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
668 place where Octave can find them.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
669
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
670 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
671 for variables or functions that are already compiled and currently
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
672 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
673 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
674 @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
675 identifier.@footnote{The @samp{.m} suffix was chosen for compatibility
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
676 with @sc{matlab}.} Once Octave finds a file with a name that matches,
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
677 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
678 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
679 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
680 file.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
681
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
682 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
683 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
684 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
685 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
686 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
687 definitions also occurs if the current working directory changes.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
688
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
689 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
690 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
691 definition without having to restart your Octave session.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
692
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
693 To avoid degrading performance unnecessarily by checking the time stamps
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
694 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
695 files in the directory tree
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
696 @file{@var{octave-home}/share/octave/@var{version}/m}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
697 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
698 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
699 assumption and provides a significant improvement in performance for the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
700 function files that are distributed with Octave.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
701
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
702 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
703 running Octave, you can improve performance by calling
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
704 @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
705 ignore the time stamps for all function files. Passing
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
706 @code{"system"} to this function resets the default behavior.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
707
5775
ace8d8d26933 [project @ 2006-04-24 19:13:06 by jwe]
jwe
parents: 5763
diff changeset
708 @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
709
8817
03b7f618ab3d include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents: 8567
diff changeset
710 @DOCSTRING(edit)
03b7f618ab3d include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents: 8567
diff changeset
711
6549
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
712 @DOCSTRING(mfilename)
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
713
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
714 @DOCSTRING(ignore_function_time_stamp)
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
715
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
716 @menu
11255
d682cd6669ac Update info-based documentation menus to include new nodes.
Rik <octave@nomad.inbox5.com>
parents: 10828
diff changeset
717 * Manipulating the Load Path::
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
718 * Subfunctions::
8221
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
719 * Private Functions::
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
720 * Overloading and Autoloading::
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
721 * Function Locking::
8221
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
722 * Function Precedence::
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
723 @end menu
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
724
11257
8d4c57258523 fix some menu problems in the manual
John W. Eaton <jwe@octave.org>
parents: 11255
diff changeset
725 @node Manipulating the Load Path
8d4c57258523 fix some menu problems in the manual
John W. Eaton <jwe@octave.org>
parents: 11255
diff changeset
726 @subsection Manipulating the Load Path
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
727
8828
8463d1a2e544 Doc fixes.
Brian Gough <bjg@network-theory.co.uk>
parents: 8817
diff changeset
728 When a function is called, Octave searches a list of directories for
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
729 a file that contains the function declaration. This list of directories
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
730 is known as the load path. By default the load path contains
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
731 a list of directories distributed with Octave plus the current
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
732 working directory. To see your current load path call the @code{path}
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
733 function without any input or output arguments.
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
734
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
735 It is possible to add or remove directories to or from the load path
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
736 using @code{addpath} and @code{rmpath}. As an example, the following
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
737 code adds @samp{~/Octave} to the load path.
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
738
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
739 @example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
740 addpath("~/Octave")
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
741 @end example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
742
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
743 @noindent
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
744 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
745
6502
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 6501
diff changeset
746 @DOCSTRING(addpath)
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 6501
diff changeset
747
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 6501
diff changeset
748 @DOCSTRING(genpath)
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 6501
diff changeset
749
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 6501
diff changeset
750 @DOCSTRING(rmpath)
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 6501
diff changeset
751
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 6501
diff changeset
752 @DOCSTRING(savepath)
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 6501
diff changeset
753
6477
a441d6681364 [project @ 2007-03-29 13:44:44 by jwe]
jwe
parents: 5775
diff changeset
754 @DOCSTRING(path)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
755
6502
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 6501
diff changeset
756 @DOCSTRING(pathdef)
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 6501
diff changeset
757
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 6501
diff changeset
758 @DOCSTRING(pathsep)
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 6501
diff changeset
759
3428
5b77cf82393c [project @ 2000-01-14 02:54:53 by jwe]
jwe
parents: 3402
diff changeset
760 @DOCSTRING(rehash)
5b77cf82393c [project @ 2000-01-14 02:54:53 by jwe]
jwe
parents: 3402
diff changeset
761
5b77cf82393c [project @ 2000-01-14 02:54:53 by jwe]
jwe
parents: 3402
diff changeset
762 @DOCSTRING(file_in_loadpath)
5b77cf82393c [project @ 2000-01-14 02:54:53 by jwe]
jwe
parents: 3402
diff changeset
763
7638
2df457529cfa implement expm1 and log1p functions
Jaroslav Hajek <highegg@gmail.com>
parents: 7588
diff changeset
764 @DOCSTRING(restoredefaultpath)
2df457529cfa implement expm1 and log1p functions
Jaroslav Hajek <highegg@gmail.com>
parents: 7588
diff changeset
765
8817
03b7f618ab3d include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents: 8567
diff changeset
766 @DOCSTRING(command_line_path)
03b7f618ab3d include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents: 8567
diff changeset
767
03b7f618ab3d include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents: 8567
diff changeset
768 @DOCSTRING(find_dir_in_path)
03b7f618ab3d include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents: 8567
diff changeset
769
6556
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
770 @node Subfunctions
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
771 @subsection Subfunctions
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
772
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
773 A function file may contain secondary functions called
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
774 @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
775 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
776 @file{f.m} containing
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
777
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
778 @example
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
779 @group
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
780 function f ()
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
781 printf ("in f, calling g\n");
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
782 g ()
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
783 endfunction
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
784 function g ()
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
785 printf ("in g, calling h\n");
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
786 h ()
6556
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
787 endfunction
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
788 function h ()
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
789 printf ("in h\n")
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
790 endfunction
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
791 @end group
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
792 @end example
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
793
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
794 @noindent
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
795 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
796 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
797 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
798 the file @file{f.m}.
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
799
8221
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
800 @node Private Functions
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
801 @subsection Private Functions
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
802
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
803 In many cases one function needs to access one or more helper
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
804 functions. If the helper function is limited to the scope of a single
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
805 function, then subfunctions as discussed above might be used. However,
8221
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
806 if a single helper function is used by more than one function, then
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
807 this is no longer possible. In this case the helper functions might
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
808 be placed in a subdirectory, called "private", of the directory in which
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
809 the functions needing access to this helper function are found.
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
810
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
811 As a simple example, consider a function @code{func1}, that calls a helper
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
812 function @code{func2} to do much of the work. For example:
8221
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
813
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
814 @example
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
815 @group
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
816 function y = func1 (x)
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
817 y = func2 (x);
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
818 endfunction
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
819 @end group
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
820 @end example
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
821
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
822 @noindent
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
823 Then if the path to @code{func1} is @code{<directory>/func1.m}, and if
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
824 @code{func2} is found in the directory @code{<directory>/private/func2.m},
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
825 then @code{func2} is only available for use of the functions, like
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
826 @code{func1}, that are found in @code{<directory>}.
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
827
6635
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
828 @node Overloading and Autoloading
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
829 @subsection Overloading and Autoloading
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
830
11396
7b563cf94d8d Remove documentation on deprecated function dispatch
Rik <octave@nomad.inbox5.com>
parents: 11392
diff changeset
831 Functions can be overloaded to work with different input arguments. For
7b563cf94d8d Remove documentation on deprecated function dispatch
Rik <octave@nomad.inbox5.com>
parents: 11392
diff changeset
832 example, the operator '+' has been overloaded in Octave to work with single,
7b563cf94d8d Remove documentation on deprecated function dispatch
Rik <octave@nomad.inbox5.com>
parents: 11392
diff changeset
833 double, uint8, int32, and many other arguments. The preferred way to overload
7b563cf94d8d Remove documentation on deprecated function dispatch
Rik <octave@nomad.inbox5.com>
parents: 11392
diff changeset
834 functions is through classes and object oriented programming
7b563cf94d8d Remove documentation on deprecated function dispatch
Rik <octave@nomad.inbox5.com>
parents: 11392
diff changeset
835 (@pxref{Function Overloading}). Occasionally, however, one needs to undo
7b563cf94d8d Remove documentation on deprecated function dispatch
Rik <octave@nomad.inbox5.com>
parents: 11392
diff changeset
836 user overloading and call the default function associated with a specific
7b563cf94d8d Remove documentation on deprecated function dispatch
Rik <octave@nomad.inbox5.com>
parents: 11392
diff changeset
837 type. The @code{builtin} function exists for this purpose.
6635
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
838
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
839 @DOCSTRING(builtin)
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
840
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
841 A single dynamically linked file might define several
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
842 functions. However, as Octave searches for functions based on the
6635
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
843 functions filename, Octave needs a manner in which to find each of the
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
844 functions in the dynamically linked file. On operating systems that
6635
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
845 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
846 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
847
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
848 However, there is at least one well known operating system that doesn't
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
849 support symbolic links. Making copies of the original file for each of
8828
8463d1a2e544 Doc fixes.
Brian Gough <bjg@network-theory.co.uk>
parents: 8817
diff changeset
850 the functions is undesirable as it increases the
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
851 amount of disk space used by Octave. Instead Octave supplies the
6635
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
852 @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
853 file a certain function will be found.
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
854
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
855 @DOCSTRING(autoload)
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
856
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
857 @node Function Locking
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
858 @subsection Function Locking
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
859
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
860 It is sometime desirable to lock a function into memory with the
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
861 @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
862 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
863 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
864 initialization.
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
865
6899
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
866 As an example,
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
867
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
868 @example
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
869 mlock ("my_function");
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
870 @end example
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
871
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
872 @noindent
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
873 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
874 @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
875 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
876 with @code{munlock}, which the following illustrates.
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
877
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
878 @example
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
879 @group
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
880 mlock ("my_function");
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
881 mislocked ("my_function")
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
882 @result{} ans = 1
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
883 munlock ("my_function");
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
884 mislocked ("my_function")
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
885 @result{} ans = 0
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
886 @end group
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
887 @end example
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
888
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
889 A common use of @code{mlock} is to prevent persistent variables from
8828
8463d1a2e544 Doc fixes.
Brian Gough <bjg@network-theory.co.uk>
parents: 8817
diff changeset
890 being removed from memory, as the following example shows:
6899
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
891
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
892 @example
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
893 @group
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
894 function count_calls()
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
895 persistent calls = 0;
7031
120f3135952f [project @ 2007-10-15 15:30:03 by jwe]
jwe
parents: 7018
diff changeset
896 printf ("'count_calls' has been called %d times\n",
120f3135952f [project @ 2007-10-15 15:30:03 by jwe]
jwe
parents: 7018
diff changeset
897 ++calls);
6899
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
898 endfunction
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
899 mlock ("count_calls");
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
900
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
901 count_calls ();
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
902 @print{} 'count_calls' has been called 1 times
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
903
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
904 clear count_calls
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
905 count_calls ();
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
906 @print{} 'count_calls' has been called 2 times
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
907 @end group
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
908 @end example
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
909
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
910 @noindent
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
911 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
912 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
913 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
914
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
915 @example
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
916 @group
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
917 function count_calls ()
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
918 mlock ();
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
919 persistent calls = 0;
7031
120f3135952f [project @ 2007-10-15 15:30:03 by jwe]
jwe
parents: 7018
diff changeset
920 printf ("'count_calls' has been called %d times\n",
120f3135952f [project @ 2007-10-15 15:30:03 by jwe]
jwe
parents: 7018
diff changeset
921 ++calls);
6899
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
922 endfunction
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
923 @end group
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
924 @end example
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
925
110c5782fe3b [project @ 2007-09-14 15:17:53 by jwe]
jwe
parents: 6778
diff changeset
926 @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
927 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
928 @code{ignore_function_time_stamp} function.
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
929
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
930 @DOCSTRING(mlock)
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
931
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
932 @DOCSTRING(munlock)
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
933
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
934 @DOCSTRING(mislocked)
c9c504d939c5 [project @ 2007-05-19 10:36:56 by dbateman]
dbateman
parents: 6570
diff changeset
935
8221
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
936 @node Function Precedence
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
937 @subsection Function Precedence
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
938
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
939 Given the numerous different ways that Octave can define a function, it
8221
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
940 is possible and even likely that multiple versions of a function, might be
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
941 defined within a particular scope. The precedence of which function will be
8221
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
942 used within a particular scope is given by
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
943
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
944 @enumerate 1
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
945 @item Subfunction
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
946 A subfunction with the required function name in the given scope.
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
947
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
948 @item Private function
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
949 A function defined within a private directory of the directory
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
950 which contains the current function.
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
951
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
952 @item Class constructor
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
953 A function that constuctors a user class as defined in chapter
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
954 @ref{Object Oriented Programming}.
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
955
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
956 @item Class method
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
957 An overloaded function of a class as in chapter
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
958 @ref{Object Oriented Programming}.
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
959
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
960 @item Legacy Dispatch
11396
7b563cf94d8d Remove documentation on deprecated function dispatch
Rik <octave@nomad.inbox5.com>
parents: 11392
diff changeset
961 An overloaded function as defined by @code{dispatch}.
8221
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
962
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
963 @item Command-line Function
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
964 A function that has been defined on the command-line.
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
965
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
966 @item Autoload function
8235
7eedf503ba1c Small makeinfo doc fixes
David Bateman <dbateman@free.fr>
parents: 8221
diff changeset
967 A function that is marked as autoloaded with @xref{doc-autoload}.
8221
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
968
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
969 @item A Function on the Path
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
970 A function that can be found on the users load-path. There can also be
8221
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
971 Oct-file, mex-file or m-file versions of this function and the precedence
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
972 between these versions are in that order.
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
973
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
974 @item Built-in function
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
975 A function that is builtin to Octave itself such as @code{numel},
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
976 @code{size}, etc.
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
977 @end enumerate
06094fa570a3 Add some documentation for the OOP code of Octave
David Bateman <dbateman@free.fr>
parents: 8074
diff changeset
978
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 4029
diff changeset
979 @node Script Files
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
980 @section Script Files
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
981
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
982 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
983 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
984 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
985 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
986
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
987 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
988 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
989 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
990 evaluated as soon as it is defined.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
991
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
992 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
993 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
994 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
995
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
996 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
997 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
998 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
999 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
1000 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
1001 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
1002 effect, like this:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1003
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1004 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1005 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1006 # Prevent Octave from thinking that this
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1007 # is a function file:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1008
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1009 1;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1010
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1011 # Define function one:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1012
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1013 function one ()
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
1014 @dots{}
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1015 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1016 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1017
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1018 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
1019 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
1020 (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
1021 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
1022 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
1023 function files.)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1024
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1025 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
1026 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
1027 message warning about any non-whitespace characters that appear after
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1028 the function definition.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1029
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1030 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
1031 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
1032 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
1033 the command line,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1034
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1035 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1036 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1037 # not a function file:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1038 1;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1039 function foo ()
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1040 do_something ();
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1041 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1042 function do_something ()
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1043 do_something_else ();
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1044 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1045 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1046 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1047
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1048 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1049 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
1050 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
1051 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
1052 function until the function is actually evaluated.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1053
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1054 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
1055 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
1056 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
1057 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
1058 @file{bar.m} in Octave's path.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1059
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1060 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1061 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1062 eval ("bar = 3");
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1063 bar
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1064 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1065 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1066
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1067 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
1068 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
1069 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
1070 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
1071 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
1072 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
1073 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
1074 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
1075 known until the function is evaluated).
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1076
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1077 Although Octave normally executes commands from script files that have
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1078 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
1079 execute commands from any file.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1080
3371
86873384cd10 [project @ 1999-11-21 17:31:07 by jwe]
jwe
parents: 3294
diff changeset
1081 @DOCSTRING(source)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1082
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1083 @node Function Handles Inline Functions and Anonymous Functions
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1084 @section Function Handles, Inline Functions, and Anonymous Functions
4933
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1085 @cindex handle, function handles
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1086 @cindex inline, inline functions
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1087 @cindex anonymous functions
4933
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1088
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1089 It can be very convenient store a function in a variable so that it
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
1090 can be passed to a different function. For example, a function that
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
1091 performs numerical minimization needs access to the function that
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
1092 should be minimized.
4933
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1093
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1094 @menu
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1095 * Function Handles::
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1096 * Anonymous Functions::
4933
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1097 * Inline Functions::
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1098 @end menu
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1099
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1100 @node Function Handles
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1101 @subsection Function Handles
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1102
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1103 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
1104 the syntax
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1105
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1106 @example
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1107 @@@var{function-name}
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1108 @end example
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1109
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1110 @noindent
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
1111 For example,
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1112
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1113 @example
6556
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
1114 f = @@sin;
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1115 @end example
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1116
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1117 @noindent
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
1118 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
1119 function @code{sin}.
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1120
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1121 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
1122 a function as an argument to another function like @code{quad} or
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
1123 @code{fsolve}. For example:
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1124
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1125 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
1126 @group
6556
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
1127 f = @@sin;
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1128 quad (f, 0, pi)
6929
d5bf6be68c87 [project @ 2007-09-28 09:21:40 by dbateman]
dbateman
parents: 6899
diff changeset
1129 @result{} 2
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
1130 @end group
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1131 @end example
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1132
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1133 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
1134 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
1135 list. If there are no arguments, you must use an empty argument list
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
1136 @samp{()}. For example:
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1137
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1138 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
1139 @group
6556
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
1140 f = @@sin;
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1141 feval (f, pi/4)
6570
49f0820425a8 [project @ 2007-04-24 23:06:56 by jwe]
jwe
parents: 6558
diff changeset
1142 @result{} 0.70711
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1143 f (pi/4)
6570
49f0820425a8 [project @ 2007-04-24 23:06:56 by jwe]
jwe
parents: 6558
diff changeset
1144 @result{} 0.70711
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
1145 @end group
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1146 @end example
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1147
4933
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1148 @DOCSTRING(functions)
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1149
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1150 @DOCSTRING(func2str)
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1151
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1152 @DOCSTRING(str2func)
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1153
6570
49f0820425a8 [project @ 2007-04-24 23:06:56 by jwe]
jwe
parents: 6558
diff changeset
1154 @node Anonymous Functions
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1155 @subsection Anonymous Functions
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1156
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1157 Anonymous functions are defined using the syntax
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1158
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1159 @example
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1160 @@(@var{argument-list}) @var{expression}
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1161 @end example
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1162
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1163 @noindent
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1164 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
1165 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
1166 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
1167 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
1168 example,
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1169
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1170 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
1171 @group
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1172 f = @@(x) x.^2;
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1173 quad (f, 0, 10)
6570
49f0820425a8 [project @ 2007-04-24 23:06:56 by jwe]
jwe
parents: 6558
diff changeset
1174 @result{} 333.33
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
1175 @end group
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1176 @end example
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 @noindent
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1179 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
1180 passes it to @code{quad},
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1181
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1182 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
1183 @group
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1184 quad (@@(x) sin (x), 0, pi)
6933
d09b8a5d09e2 [project @ 2007-09-28 10:59:51 by dbateman]
dbateman
parents: 6929
diff changeset
1185 @result{} 2
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
1186 @end group
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1187 @end example
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1188
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1189 @noindent
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1190 wraps another function, and
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1191
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1192 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
1193 @group
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1194 a = 1;
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1195 b = 2;
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1196 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
1197 @result{} 0.13867
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
1198 @end group
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1199 @end example
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1200
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1201 @noindent
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1202 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
1203 @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
1204 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
1205 environment.
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1206
4933
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1207 @node Inline Functions
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1208 @subsection Inline Functions
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1209
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1210 An inline function is created from a string containing the function
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
1211 body using the @code{inline} function. The following code defines the
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1212 function @math{f(x) = x^2 + 2}.
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1213
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1214 @example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1215 f = inline("x^2 + 2");
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1216 @end example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1217
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1218 @noindent
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1219 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
1220 writing @code{f(x)}.
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1221
4933
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1222 @DOCSTRING(inline)
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1223
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1224 @DOCSTRING(argnames)
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1225
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1226 @DOCSTRING(formula)
cd58733c326b [project @ 2004-08-05 16:08:28 by jwe]
jwe
parents: 4718
diff changeset
1227
7984
bbaa5d7d0143 Some documentation updates
David Bateman <dbateman@free.fr>
parents: 7638
diff changeset
1228 @DOCSTRING(symvar)
bbaa5d7d0143 Some documentation updates
David Bateman <dbateman@free.fr>
parents: 7638
diff changeset
1229
6549
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
1230 @node Commands
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
1231 @section Commands
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
1232
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1233 Commands are a special class of functions that only accept string
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
1234 input arguments. A command can be called as an ordinary function, but
11392
757efa1d7e2a Remove deprecated functions from manual.
Rik <octave@nomad.inbox5.com>
parents: 11257
diff changeset
1235 it can also be called without the parentheses. For example,
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1236
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1237 @example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1238 my_command hello world
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1239 @end example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1240
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1241 @noindent
11392
757efa1d7e2a Remove deprecated functions from manual.
Rik <octave@nomad.inbox5.com>
parents: 11257
diff changeset
1242 is equivalent to
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1243
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1244 @example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1245 my_command("hello", "world")
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1246 @end example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1247
11392
757efa1d7e2a Remove deprecated functions from manual.
Rik <octave@nomad.inbox5.com>
parents: 11257
diff changeset
1248 @noindent
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1249 The general form of a command call is
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1250
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1251 @example
11392
757efa1d7e2a Remove deprecated functions from manual.
Rik <octave@nomad.inbox5.com>
parents: 11257
diff changeset
1252 @var{cmdname} @var{arg1} @var{arg2} @dots{}
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1253 @end example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1254
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1255 @noindent
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1256 which translates directly to
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1257
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1258 @example
11392
757efa1d7e2a Remove deprecated functions from manual.
Rik <octave@nomad.inbox5.com>
parents: 11257
diff changeset
1259 @var{cmdname} ("@var{arg1}", "@var{arg2}", @dots{})
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1260 @end example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1261
11392
757efa1d7e2a Remove deprecated functions from manual.
Rik <octave@nomad.inbox5.com>
parents: 11257
diff changeset
1262 Any regular function can be used as a command if it accepts string input
757efa1d7e2a Remove deprecated functions from manual.
Rik <octave@nomad.inbox5.com>
parents: 11257
diff changeset
1263 arguments. For example:
11573
6f8ffe2c6f76 Grammarcheck txi files for 3.4 release.
Rik <octave@nomad.inbox5.com>
parents: 11547
diff changeset
1264
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1265 @example
11392
757efa1d7e2a Remove deprecated functions from manual.
Rik <octave@nomad.inbox5.com>
parents: 11257
diff changeset
1266 @group
757efa1d7e2a Remove deprecated functions from manual.
Rik <octave@nomad.inbox5.com>
parents: 11257
diff changeset
1267 toupper lower_case_arg
757efa1d7e2a Remove deprecated functions from manual.
Rik <octave@nomad.inbox5.com>
parents: 11257
diff changeset
1268 @result{} ans = LOWER_CASE_ARG
757efa1d7e2a Remove deprecated functions from manual.
Rik <octave@nomad.inbox5.com>
parents: 11257
diff changeset
1269 @end group
6638
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1270 @end example
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1271
15837c5982cb [project @ 2007-05-21 19:24:19 by jwe]
jwe
parents: 6635
diff changeset
1272 One difficulty of commands occurs when one of the string input arguments
11392
757efa1d7e2a Remove deprecated functions from manual.
Rik <octave@nomad.inbox5.com>
parents: 11257
diff changeset
1273 is stored in a variable. Because Octave can't tell the difference between
757efa1d7e2a Remove deprecated functions from manual.
Rik <octave@nomad.inbox5.com>
parents: 11257
diff changeset
1274 a variable name and an ordinary string, it is not possible to pass a
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
1275 variable as input to a command. In such a situation a command must be
11392
757efa1d7e2a Remove deprecated functions from manual.
Rik <octave@nomad.inbox5.com>
parents: 11257
diff changeset
1276 called as a function. For example:
6549
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
1277
11392
757efa1d7e2a Remove deprecated functions from manual.
Rik <octave@nomad.inbox5.com>
parents: 11257
diff changeset
1278 @example
757efa1d7e2a Remove deprecated functions from manual.
Rik <octave@nomad.inbox5.com>
parents: 11257
diff changeset
1279 @group
757efa1d7e2a Remove deprecated functions from manual.
Rik <octave@nomad.inbox5.com>
parents: 11257
diff changeset
1280 strvar = "hello world";
757efa1d7e2a Remove deprecated functions from manual.
Rik <octave@nomad.inbox5.com>
parents: 11257
diff changeset
1281 toupper strvar
757efa1d7e2a Remove deprecated functions from manual.
Rik <octave@nomad.inbox5.com>
parents: 11257
diff changeset
1282 @result{} ans = STRVAR
757efa1d7e2a Remove deprecated functions from manual.
Rik <octave@nomad.inbox5.com>
parents: 11257
diff changeset
1283 toupper (strvar)
757efa1d7e2a Remove deprecated functions from manual.
Rik <octave@nomad.inbox5.com>
parents: 11257
diff changeset
1284 @result{} ans = HELLO WORLD
757efa1d7e2a Remove deprecated functions from manual.
Rik <octave@nomad.inbox5.com>
parents: 11257
diff changeset
1285 @end group
757efa1d7e2a Remove deprecated functions from manual.
Rik <octave@nomad.inbox5.com>
parents: 11257
diff changeset
1286 @end example
6549
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
1287
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6510
diff changeset
1288
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 4029
diff changeset
1289 @node Organization of Functions
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1290 @section Organization of Functions Distributed with Octave
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1291
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1292 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
1293 They are loosely organized by topic, in subdirectories of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1294 @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
1295 to find them.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1296
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1297 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
1298 types of functions you will find there.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1299
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1300 @table @file
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1301 @item audio
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1302 Functions for playing and recording sounds.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1303
12632
2dbac27e0e40 doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents: 12629
diff changeset
1304 @item deprecated
2dbac27e0e40 doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents: 12629
diff changeset
1305 Out-of-date functions which will eventually be removed from Octave.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1306
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1307 @item elfun
12632
2dbac27e0e40 doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents: 12629
diff changeset
1308 Elementary functions, principally trigonometric.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1309
12632
2dbac27e0e40 doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents: 12629
diff changeset
1310 @item @@ftp
2dbac27e0e40 doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents: 12629
diff changeset
1311 Class functions for the FTP object.
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1312
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1313 @item general
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1314 Miscellaneous matrix manipulations, like @code{flipud}, @code{rot90},
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1315 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
1316 @code{ismatrix}, @code{nargchk}, etc.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1317
12632
2dbac27e0e40 doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents: 12629
diff changeset
1318 @item geometry
2dbac27e0e40 doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents: 12629
diff changeset
1319 Functions related to Delaunay triangulation.
2dbac27e0e40 doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents: 12629
diff changeset
1320
2dbac27e0e40 doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents: 12629
diff changeset
1321 @item help
2dbac27e0e40 doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents: 12629
diff changeset
1322 Functions for Octave's built-in help system.
2dbac27e0e40 doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents: 12629
diff changeset
1323
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1324 @item image
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1325 Image processing tools. These functions require the X Window System.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1326
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1327 @item io
8325
b93ac0586e4b spelling corrections
Brian Gough<bjg@network-theory.co.uk>
parents: 8286
diff changeset
1328 Input-output functions.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1329
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1330 @item linear-algebra
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1331 Functions for linear algebra.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1332
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1333 @item miscellaneous
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1334 Functions that don't really belong anywhere else.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1335
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1336 @item optimization
12642
f96b9b9f141b doc: Periodic grammarcheck and spellcheck of documentation.
Rik <octave@nomad.inbox5.com>
parents: 12632
diff changeset
1337 Functions related to minimization, optimization, and root finding.
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1338
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1339 @item path
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1340 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
1341
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1342 @item pkg
12632
2dbac27e0e40 doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents: 12629
diff changeset
1343 Package manager for installing external packages of functions in Octave.
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1344
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1345 @item plot
6556
8810bbf321ce [project @ 2007-04-20 18:39:40 by jwe]
jwe
parents: 6554
diff changeset
1346 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
1347
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1348 @item polynomial
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1349 Functions for manipulating polynomials.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1350
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1351 @item set
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1352 Functions for creating and manipulating sets of unique values.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1353
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1354 @item signal
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1355 Functions for signal processing applications.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1356
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1357 @item sparse
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1358 Functions for handling sparse matrices.
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1359
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1360 @item specfun
12632
2dbac27e0e40 doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents: 12629
diff changeset
1361 Special functions such as @code{bessel} or @code{factor}.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1362
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1363 @item special-matrix
12632
2dbac27e0e40 doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents: 12629
diff changeset
1364 Functions that create special matrix forms such as Hilbert or Vandermonde
2dbac27e0e40 doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents: 12629
diff changeset
1365 matrices.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1366
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1367 @item startup
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1368 Octave's system-wide startup file.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1369
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1370 @item statistics
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1371 Statistical functions.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1372
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1373 @item strings
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1374 Miscellaneous string-handling functions.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1375
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1376 @item testfun
12632
2dbac27e0e40 doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents: 12629
diff changeset
1377 Functions for performing unit tests on other functions.
6554
5dde4dc2bcaf [project @ 2007-04-20 17:16:50 by jwe]
jwe
parents: 6549
diff changeset
1378
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1379 @item time
12632
2dbac27e0e40 doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents: 12629
diff changeset
1380 Functions related to time and date processing.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1381 @end table