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