Mercurial > octave-antonio
annotate doc/interpreter/func.txi @ 8074:595028fcf65d
doc fix
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 28 Aug 2008 15:31:01 -0400 |
parents | 3b6e7d04b614 |
children | 06094fa570a3 |
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 |
4167 | 338 @node Variable-length Argument Lists |
3294 | 339 @section Variable-length Argument Lists |
4933 | 340 @cindex variable-length argument lists |
8074 | 341 @vrindex @code{varargin} |
8072 | 342 |
6510 | 343 Sometimes the number of input arguments is not known when the function |
344 is defined. As an example think of a function that returns the smallest | |
345 of all its input arguments. For example, | |
346 | |
347 @example | |
348 a = smallest (1, 2, 3); | |
349 b = smallest (1, 2, 3, 4); | |
350 @end example | |
351 | |
352 @noindent | |
353 In this example both @code{a} and @code{b} would be 1. One way to write | |
354 the @code{smallest} function is | |
355 | |
356 @example | |
357 function val = smallest (arg1, arg2, arg3, arg4, arg5) | |
358 @var{body} | |
359 endfunction | |
360 @end example | |
361 | |
362 @noindent | |
363 and then use the value of @code{nargin} to determine which of the input | |
364 arguments should be considered. The problem with this approach is | |
365 that it can only handle a limited number of input arguments. | |
366 | |
7588
cbedf652a752
doc fix for varargin and varargout change
John W. Eaton <jwe@octave.org>
parents:
7031
diff
changeset
|
367 If the special parameter name @code{varargin} appears at the end of a |
cbedf652a752
doc fix for varargin and varargout change
John W. Eaton <jwe@octave.org>
parents:
7031
diff
changeset
|
368 function parameter list it indicates that the function takes a variable |
cbedf652a752
doc fix for varargin and varargout change
John W. Eaton <jwe@octave.org>
parents:
7031
diff
changeset
|
369 number of input arguments. Using @code{varargin} the function |
6510 | 370 looks like this |
371 | |
372 @example | |
373 function val = smallest (varargin) | |
374 @var{body} | |
375 endfunction | |
376 @end example | |
377 | |
378 @noindent | |
379 In the function body the input arguments can be accessed through the | |
380 variable @code{varargin}. This variable is a cell array containing | |
381 all the input arguments. @xref{Cell Arrays}, for details on working | |
382 with cell arrays. The @code{smallest} function can now be defined | |
383 like this | |
384 | |
385 @example | |
386 function val = smallest (varargin) | |
387 val = min ([varargin@{:@}]); | |
388 endfunction | |
389 @end example | |
390 | |
391 @noindent | |
392 This implementation handles any number of input arguments, but it's also | |
393 a very simple solution to the problem. | |
394 | |
395 A slightly more complex example of @code{varargin} is a function | |
396 @code{print_arguments} that prints all input arguments. Such a function | |
397 can be defined like this | |
398 | |
399 @example | |
400 function print_arguments (varargin) | |
401 for i = 1:length (varargin) | |
402 printf ("Input argument %d: ", i); | |
403 disp (varargin@{i@}); | |
404 endfor | |
405 endfunction | |
406 @end example | |
407 | |
408 @noindent | |
409 This function produces output like this | |
410 | |
411 @example | |
412 @group | |
413 print_arguments (1, "two", 3); | |
414 @print{} Input argument 1: 1 | |
415 @print{} Input argument 2: two | |
416 @print{} Input argument 3: 3 | |
417 @end group | |
418 @end example | |
3294 | 419 |
6558 | 420 @DOCSTRING(parseparams) |
421 | |
4167 | 422 @node Variable-length Return Lists |
3294 | 423 @section Variable-length Return Lists |
4933 | 424 @cindex variable-length return lists |
8074 | 425 @vrindex @code{varargout} |
8072 | 426 |
6510 | 427 It is possible to return a variable number of output arguments from a |
428 function using a syntax that's similar to the one used with the | |
7588
cbedf652a752
doc fix for varargin and varargout change
John W. Eaton <jwe@octave.org>
parents:
7031
diff
changeset
|
429 special @code{varargin} parameter name. To let a function return a |
cbedf652a752
doc fix for varargin and varargout change
John W. Eaton <jwe@octave.org>
parents:
7031
diff
changeset
|
430 variable number of output arguments the special output parameter name |
cbedf652a752
doc fix for varargin and varargout change
John W. Eaton <jwe@octave.org>
parents:
7031
diff
changeset
|
431 @code{varargout} is used. As with @code{varargin}, @code{varargout} is |
cbedf652a752
doc fix for varargin and varargout change
John W. Eaton <jwe@octave.org>
parents:
7031
diff
changeset
|
432 a cell array that will contain the requested output arguments. |
6510 | 433 |
434 As an example the following function sets the first output argument to | |
435 1, the second to 2, and so on. | |
436 | |
437 @example | |
438 function varargout = one_to_n () | |
439 for i = 1:nargout | |
440 varargout@{i@} = i; | |
441 endfor | |
442 endfunction | |
443 @end example | |
444 | |
445 @noindent | |
446 When called this function returns values like this | |
447 | |
448 @example | |
449 @group | |
450 [a, b, c] = one_to_n () | |
451 @result{} a = 1 | |
452 @result{} b = 2 | |
453 @result{} c = 3 | |
454 @end group | |
455 @end example | |
3294 | 456 |
7588
cbedf652a752
doc fix for varargin and varargout change
John W. Eaton <jwe@octave.org>
parents:
7031
diff
changeset
|
457 If @code{varargin} (@code{varargout}) does not appear as the last |
cbedf652a752
doc fix for varargin and varargout change
John W. Eaton <jwe@octave.org>
parents:
7031
diff
changeset
|
458 element of the input (output) parameter list, then it is not special, |
cbedf652a752
doc fix for varargin and varargout change
John W. Eaton <jwe@octave.org>
parents:
7031
diff
changeset
|
459 and is handled the same as any other parameter name. |
cbedf652a752
doc fix for varargin and varargout change
John W. Eaton <jwe@octave.org>
parents:
7031
diff
changeset
|
460 |
6558 | 461 @DOCSTRING(deal) |
462 | |
4167 | 463 @node Returning From a Function |
3294 | 464 @section Returning From a Function |
465 | |
466 The body of a user-defined function can contain a @code{return} statement. | |
467 This statement returns control to the rest of the Octave program. It | |
468 looks like this: | |
469 | |
470 @example | |
471 return | |
472 @end example | |
473 | |
474 Unlike the @code{return} statement in C, Octave's @code{return} | |
475 statement cannot be used to return a value from a function. Instead, | |
476 you must assign values to the list of return variables that are part of | |
477 the @code{function} statement. The @code{return} statement simply makes | |
478 it easier to exit a function from a deeply nested loop or conditional | |
479 statement. | |
480 | |
481 Here is an example of a function that checks to see if any elements of a | |
482 vector are nonzero. | |
483 | |
484 @example | |
485 @group | |
486 function retval = any_nonzero (v) | |
487 retval = 0; | |
488 for i = 1:length (v) | |
489 if (v (i) != 0) | |
490 retval = 1; | |
491 return; | |
492 endif | |
493 endfor | |
494 printf ("no nonzero elements found\n"); | |
495 endfunction | |
496 @end group | |
497 @end example | |
498 | |
499 Note that this function could not have been written using the | |
500 @code{break} statement to exit the loop once a nonzero value is found | |
501 without adding extra logic to avoid printing the message if the vector | |
502 does contain a nonzero element. | |
503 | |
5763 | 504 @deffn {Keyword} return |
3294 | 505 When Octave encounters the keyword @code{return} inside a function or |
5016 | 506 script, it returns control to the caller immediately. At the top level, |
3294 | 507 the return statement is ignored. A @code{return} statement is assumed |
508 at the end of every function definition. | |
5763 | 509 @end deffn |
3294 | 510 |
3371 | 511 @DOCSTRING(return_last_computed_value) |
3294 | 512 |
6510 | 513 @node Default Arguments |
514 @section Default Arguments | |
515 @cindex default arguments | |
516 | |
517 Since Octave supports variable number of input arguments, it is very useful | |
518 to assign default values to some input arguments. When an input argument | |
519 is declared in the argument list it is possible to assign a default | |
520 value to the argument like this | |
521 | |
522 @example | |
523 function @var{name} (@var{arg1} = @var{val1}, @dots{}) | |
524 @var{body} | |
525 endfunction | |
526 @end example | |
527 | |
528 @noindent | |
529 If no value is assigned to @var{arg1} by the user, it will have the | |
530 value @var{val1}. | |
531 | |
532 As an example, the following function implements a variant of the classic | |
533 ``Hello, World'' program. | |
534 @example | |
535 function hello (who = "World") | |
536 printf ("Hello, %s!\n", who); | |
537 endfunction | |
538 @end example | |
539 | |
540 @noindent | |
541 When called without an input argument the function prints the following | |
542 @example | |
543 @group | |
544 hello (); | |
545 @print{} Hello, World! | |
546 @end group | |
547 @end example | |
548 | |
549 @noindent | |
550 and when it's called with an input argument it prints the following | |
551 @example | |
552 @group | |
553 hello ("Beautiful World of Free Software"); | |
554 @print{} Hello, Beautiful World of Free Software! | |
555 @end group | |
556 @end example | |
557 | |
558 Sometimes it is useful to explicitly tell Octave to use the default value | |
559 of an input argument. This can be done writing a @samp{:} as the value | |
560 of the input argument when calling the function. | |
561 @example | |
562 @group | |
563 hello (:); | |
564 @print{} Hello, World! | |
565 @end group | |
566 @end example | |
567 | |
4167 | 568 @node Function Files |
3294 | 569 @section Function Files |
570 @cindex function file | |
571 | |
572 Except for simple one-shot programs, it is not practical to have to | |
573 define all the functions you need each time you need them. Instead, you | |
574 will normally want to save them in a file so that you can easily edit | |
575 them, and save them for use at a later time. | |
576 | |
577 Octave does not require you to load function definitions from files | |
578 before using them. You simply need to put the function definitions in a | |
579 place where Octave can find them. | |
580 | |
581 When Octave encounters an identifier that is undefined, it first looks | |
582 for variables or functions that are already compiled and currently | |
583 listed in its symbol table. If it fails to find a definition there, it | |
6556 | 584 searches a list of directories (the @dfn{path}) for files ending in |
6554 | 585 @file{.m} that have the same base name as the undefined |
586 identifier.@footnote{The @samp{.m} suffix was chosen for compatibility | |
587 with @sc{Matlab}.} Once Octave finds a file with a name that matches, | |
588 the contents of the file are read. If it defines a @emph{single} | |
589 function, it is compiled and executed. @xref{Script Files}, for more | |
590 information about how you can define more than one function in a single | |
591 file. | |
3294 | 592 |
593 When Octave defines a function from a function file, it saves the full | |
6554 | 594 name of the file it read and the time stamp on the file. If the time |
595 stamp on the file changes, Octave may reload the file. When Octave is | |
596 running interactively, time stamp checking normally happens at most once | |
597 each time Octave prints the prompt. Searching for new function | |
598 definitions also occurs if the current working directory changes. | |
3294 | 599 |
600 Checking the time stamp allows you to edit the definition of a function | |
601 while Octave is running, and automatically use the new function | |
6554 | 602 definition without having to restart your Octave session. |
3294 | 603 |
604 To avoid degrading performance unnecessarily by checking the time stamps | |
605 on functions that are not likely to change, Octave assumes that function | |
606 files in the directory tree | |
607 @file{@var{octave-home}/share/octave/@var{version}/m} | |
608 will not change, so it doesn't have to check their time stamps every time the | |
609 functions defined in those files are used. This is normally a very good | |
610 assumption and provides a significant improvement in performance for the | |
611 function files that are distributed with Octave. | |
612 | |
613 If you know that your own function files will not change while you are | |
6554 | 614 running Octave, you can improve performance by calling |
615 @code{ignore_function_time_stamp ("all")}, so that Octave will | |
616 ignore the time stamps for all function files. Passing | |
617 @code{"system"} to this function resets the default behavior. | |
3294 | 618 |
5775 | 619 @c FIXME -- note about time stamps on files in NFS environments? |
3294 | 620 |
6549 | 621 @DOCSTRING(mfilename) |
622 | |
6638 | 623 @DOCSTRING(ignore_function_time_stamp) |
624 | |
625 @menu | |
626 * Manipulating the load path:: | |
627 * Subfunctions:: | |
628 * Overloading and Autoloading:: | |
629 * Function Locking:: | |
630 @end menu | |
631 | |
632 @node Manipulating the load path | |
633 @subsection Manipulating the load path | |
634 | |
635 When a function is called Octave searches a list of directories for | |
636 a file that contains the function declaration. This list of directories | |
637 is known as the load path. By default the load path contains | |
638 a list of directories distributed with Octave plus the current | |
639 working directory. To see your current load path call the @code{path} | |
640 function without any input or output arguments. | |
641 | |
642 It is possible to add or remove directories to or from the load path | |
643 using the @code{addpath} and @code{rmpath}. As an example, the following | |
644 code adds @samp{~/Octave} to the load path. | |
645 | |
646 @example | |
647 addpath("~/Octave") | |
648 @end example | |
649 | |
650 @noindent | |
651 After this the directory @samp{~/Octave} will be searched for functions. | |
652 | |
6502 | 653 @DOCSTRING(addpath) |
654 | |
655 @DOCSTRING(genpath) | |
656 | |
657 @DOCSTRING(rmpath) | |
658 | |
659 @DOCSTRING(savepath) | |
660 | |
6477 | 661 @DOCSTRING(path) |
3294 | 662 |
6502 | 663 @DOCSTRING(pathdef) |
664 | |
665 @DOCSTRING(pathsep) | |
666 | |
3428 | 667 @DOCSTRING(rehash) |
668 | |
669 @DOCSTRING(file_in_loadpath) | |
670 | |
7638
2df457529cfa
implement expm1 and log1p functions
Jaroslav Hajek <highegg@gmail.com>
parents:
7588
diff
changeset
|
671 @DOCSTRING(restoredefaultpath) |
2df457529cfa
implement expm1 and log1p functions
Jaroslav Hajek <highegg@gmail.com>
parents:
7588
diff
changeset
|
672 |
6556 | 673 @node Subfunctions |
674 @subsection Subfunctions | |
675 | |
676 A function file may contain secondary functions called | |
677 @dfn{subfunctions}. These secondary functions are only visible to the | |
678 other functions in the same function file. For example, a file | |
679 @file{f.m} containing | |
680 | |
681 @example | |
682 @group | |
683 function f () | |
684 printf ("in f, calling g\n"); | |
685 g () | |
686 endfunction | |
687 function g () | |
688 printf ("in g, calling h\n"); | |
6638 | 689 h () |
6556 | 690 endfunction |
691 function h () | |
692 printf ("in h\n") | |
693 endfunction | |
694 @end group | |
695 @end example | |
696 | |
697 @noindent | |
698 defines a main function @code{f} and two subfunctions. The | |
699 subfunctions @code{g} and @code{h} may only be called from the main | |
700 function @code{f} or from the other subfunctions, but not from outside | |
701 the file @file{f.m}. | |
702 | |
6635 | 703 @node Overloading and Autoloading |
704 @subsection Overloading and Autoloading | |
705 | |
706 The @code{dispatch} function can be used to alias one function name to | |
707 another. It can be used to alias all calls to a particular function name | |
708 to another function, or the alias can be limited to only a particular | |
709 variable type. Consider the example | |
710 | |
711 @example | |
712 @group | |
713 function y = spsin (x) | |
714 printf ("Calling spsin\n"); | |
715 fflush(stdout); | |
716 y = spfun ("sin", x); | |
717 endfunction | |
718 | |
719 dispatch ("sin", "spsin", "sparse matrix"); | |
720 y0 = sin(eye(3)); | |
721 y1 = sin(speye(3)); | |
722 @end group | |
723 @end example | |
724 | |
725 @noindent | |
726 Which aliases the @code{spsin} to @code{sin}, but only for real sparse | |
727 matrices. Note that the builtin @code{sin} already correctly treats | |
728 sparse matrices and so this example is only illustrative. | |
729 | |
730 @DOCSTRING(dispatch) | |
731 | |
732 @DOCSTRING(builtin) | |
733 | |
734 A single dynamically linked file might define several | |
735 functions. However, as Octave searches for functions based on the | |
736 functions filename, Octave needs a manner in which to find each of the | |
737 functions in the dynamically linked file. On operating systems that | |
738 support symbolic links, it is possible to create a symbolic link to the | |
739 original file for each of the functions which it contains. | |
740 | |
741 However, there is at least one well known operating system that doesn't | |
742 support symbolic links. Making copies of the original file for each of | |
743 the functions is also possible, but is undesirable as it multiples the | |
744 amount of disk space used by Octave. Instead Octave supplies the | |
745 @code{autoload} function, that permits the user to define in which | |
746 file a certain function will be found. | |
747 | |
748 @DOCSTRING(autoload) | |
749 | |
750 @node Function Locking | |
751 @subsection Function Locking | |
752 | |
753 It is sometime desirable to lock a function into memory with the | |
754 @code{mlock} function. This is typically used for dynamically linked | |
6899 | 755 functions in Oct-files or mex-files that contain some initialization, |
756 and it is desirable that calling @code{clear} does not remove this | |
6635 | 757 initialization. |
758 | |
6899 | 759 As an example, |
760 | |
761 @example | |
762 mlock ("my_function"); | |
763 @end example | |
764 | |
765 @noindent | |
766 prevents @code{my_function} from being removed from memory, even if | |
767 @code{clear} is called. It is possible to determine if a function is | |
768 locked into memory with the @code{mislocked}, and to unlock a function | |
769 with @code{munlock}, which the following illustrates. | |
770 | |
771 @example | |
772 @group | |
773 mlock ("my_function"); | |
774 mislocked ("my_function") | |
775 @result{} ans = 1 | |
776 munlock ("my_function"); | |
777 mislocked ("my_function") | |
778 @result{} ans = 0 | |
779 @end group | |
780 @end example | |
781 | |
782 A common use of @code{mlock} is to prevent persistent variables from | |
783 being removed from memory, as the following example shows. | |
784 | |
785 @example | |
786 @group | |
787 function count_calls() | |
788 persistent calls = 0; | |
7031 | 789 printf ("'count_calls' has been called %d times\n", |
790 ++calls); | |
6899 | 791 endfunction |
792 mlock ("count_calls"); | |
793 | |
794 count_calls (); | |
795 @print{} 'count_calls' has been called 1 times | |
796 | |
797 clear count_calls | |
798 count_calls (); | |
799 @print{} 'count_calls' has been called 2 times | |
800 @end group | |
801 @end example | |
802 | |
803 @noindent | |
804 It is, however, often inconvenient to lock a function from the prompt, | |
805 so it is also possible to lock a function from within its body. This | |
806 is simply done by calling @code{mlock} from within the function. | |
807 | |
808 @example | |
809 @group | |
810 function count_calls () | |
811 mlock (); | |
812 persistent calls = 0; | |
7031 | 813 printf ("'count_calls' has been called %d times\n", |
814 ++calls); | |
6899 | 815 endfunction |
816 @end group | |
817 @end example | |
818 | |
819 @code{mlock} might equally be used to prevent changes to a function from having | |
6635 | 820 effect in Octave, though a similar effect can be had with the |
821 @code{ignore_function_time_stamp} function. | |
822 | |
823 @DOCSTRING(mlock) | |
824 | |
825 @DOCSTRING(munlock) | |
826 | |
827 @DOCSTRING(mislocked) | |
828 | |
4167 | 829 @node Script Files |
3294 | 830 @section Script Files |
831 | |
832 A script file is a file containing (almost) any sequence of Octave | |
833 commands. It is read and evaluated just as if you had typed each | |
834 command at the Octave prompt, and provides a convenient way to perform a | |
835 sequence of commands that do not logically belong inside a function. | |
836 | |
837 Unlike a function file, a script file must @emph{not} begin with the | |
838 keyword @code{function}. If it does, Octave will assume that it is a | |
839 function file, and that it defines a single function that should be | |
840 evaluated as soon as it is defined. | |
841 | |
842 A script file also differs from a function file in that the variables | |
843 named in a script file are not local variables, but are in the same | |
844 scope as the other variables that are visible on the command line. | |
845 | |
846 Even though a script file may not begin with the @code{function} | |
847 keyword, it is possible to define more than one function in a single | |
848 script file and load (but not execute) all of them at once. To do | |
849 this, the first token in the file (ignoring comments and other white | |
850 space) must be something other than @code{function}. If you have no | |
851 other statements to evaluate, you can use a statement that has no | |
852 effect, like this: | |
853 | |
854 @example | |
855 @group | |
856 # Prevent Octave from thinking that this | |
857 # is a function file: | |
858 | |
859 1; | |
860 | |
861 # Define function one: | |
862 | |
863 function one () | |
864 ... | |
865 @end group | |
866 @end example | |
867 | |
868 To have Octave read and compile these functions into an internal form, | |
6638 | 869 you need to make sure that the file is in Octave's load path |
6477 | 870 (accessible through the @code{path} function), then simply type the |
871 base name of the file that contains the commands. (Octave uses the | |
872 same rules to search for script files as it does to search for | |
873 function files.) | |
3294 | 874 |
875 If the first token in a file (ignoring comments) is @code{function}, | |
876 Octave will compile the function and try to execute it, printing a | |
877 message warning about any non-whitespace characters that appear after | |
878 the function definition. | |
879 | |
880 Note that Octave does not try to look up the definition of any identifier | |
881 until it needs to evaluate it. This means that Octave will compile the | |
882 following statements if they appear in a script file, or are typed at | |
883 the command line, | |
884 | |
885 @example | |
886 @group | |
887 # not a function file: | |
888 1; | |
889 function foo () | |
890 do_something (); | |
891 endfunction | |
892 function do_something () | |
893 do_something_else (); | |
894 endfunction | |
895 @end group | |
896 @end example | |
897 | |
898 @noindent | |
899 even though the function @code{do_something} is not defined before it is | |
900 referenced in the function @code{foo}. This is not an error because | |
901 Octave does not need to resolve all symbols that are referenced by a | |
902 function until the function is actually evaluated. | |
903 | |
904 Since Octave doesn't look for definitions until they are needed, the | |
905 following code will always print @samp{bar = 3} whether it is typed | |
906 directly on the command line, read from a script file, or is part of a | |
907 function body, even if there is a function or script file called | |
6477 | 908 @file{bar.m} in Octave's path. |
3294 | 909 |
910 @example | |
911 @group | |
912 eval ("bar = 3"); | |
913 bar | |
914 @end group | |
915 @end example | |
916 | |
917 Code like this appearing within a function body could fool Octave if | |
918 definitions were resolved as the function was being compiled. It would | |
919 be virtually impossible to make Octave clever enough to evaluate this | |
920 code in a consistent fashion. The parser would have to be able to | |
921 perform the call to @code{eval} at compile time, and that would be | |
922 impossible unless all the references in the string to be evaluated could | |
923 also be resolved, and requiring that would be too restrictive (the | |
924 string might come from user input, or depend on things that are not | |
925 known until the function is evaluated). | |
926 | |
927 Although Octave normally executes commands from script files that have | |
928 the name @file{@var{file}.m}, you can use the function @code{source} to | |
929 execute commands from any file. | |
930 | |
3371 | 931 @DOCSTRING(source) |
3294 | 932 |
6638 | 933 @node Function Handles Inline Functions and Anonymous Functions |
934 @section Function Handles, Inline Functions, and Anonymous Functions | |
4933 | 935 @cindex handle, function handles |
936 @cindex inline, inline functions | |
6638 | 937 @cindex anonymous functions |
4933 | 938 |
6638 | 939 It can be very convenient store a function in a variable so that it |
940 can be passed to a different function. For example, a function that | |
941 performs numerical minimisation needs access to the function that | |
942 should be minimised. | |
4933 | 943 |
944 @menu | |
945 * Function Handles:: | |
6554 | 946 * Anonymous Functions:: |
4933 | 947 * Inline Functions:: |
948 @end menu | |
949 | |
950 @node Function Handles | |
951 @subsection Function Handles | |
952 | |
6554 | 953 A function handle is a pointer to another function and is defined with |
954 the syntax | |
955 | |
956 @example | |
957 @@@var{function-name} | |
958 @end example | |
959 | |
960 @noindent | |
961 For example | |
962 | |
963 @example | |
6556 | 964 f = @@sin; |
6554 | 965 @end example |
966 | |
967 @noindent | |
6570 | 968 Creates a function handle called @code{f} that refers to the |
6554 | 969 function @code{sin}. |
970 | |
971 Function handles are used to call other functions indirectly, or to pass | |
972 a function as an argument to another function like @code{quad} or | |
973 @code{fsolve}. For example | |
974 | |
975 @example | |
6556 | 976 f = @@sin; |
6554 | 977 quad (f, 0, pi) |
6929 | 978 @result{} 2 |
6554 | 979 @end example |
980 | |
981 You may use @code{feval} to call a function using function handle, or | |
6570 | 982 simply write the name of the function handle followed by an argument |
6554 | 983 list. If there are no arguments, you must use an empty argument list |
984 @samp{()}. For example | |
985 | |
986 @example | |
6556 | 987 f = @@sin; |
6554 | 988 feval (f, pi/4) |
6570 | 989 @result{} 0.70711 |
6554 | 990 f (pi/4) |
6570 | 991 @result{} 0.70711 |
6554 | 992 @end example |
993 | |
4933 | 994 @DOCSTRING(functions) |
995 | |
996 @DOCSTRING(func2str) | |
997 | |
998 @DOCSTRING(str2func) | |
999 | |
6570 | 1000 @node Anonymous Functions |
6554 | 1001 @subsection Anonymous Functions |
1002 | |
1003 Anonymous functions are defined using the syntax | |
1004 | |
1005 @example | |
1006 @@(@var{argument-list}) @var{expression} | |
1007 @end example | |
1008 | |
1009 @noindent | |
1010 Any variables that are not found in the argument list are inherited from | |
1011 the enclosing scope. Anonymous functions are useful for creating simple | |
1012 unnamed functions from expressions or for wrapping calls to other | |
1013 functions to adapt them for use by functions like @code{quad}. For | |
1014 example, | |
1015 | |
1016 @example | |
1017 f = @@(x) x.^2; | |
1018 quad (f, 0, 10) | |
6570 | 1019 @result{} 333.33 |
6554 | 1020 @end example |
1021 | |
1022 @noindent | |
1023 creates a simple unnamed function from the expression @code{x.^2} and | |
1024 passes it to @code{quad}, | |
1025 | |
1026 @example | |
1027 quad (@@(x) sin (x), 0, pi) | |
6933 | 1028 @result{} 2 |
6554 | 1029 @end example |
1030 | |
1031 @noindent | |
1032 wraps another function, and | |
1033 | |
1034 @example | |
1035 a = 1; | |
1036 b = 2; | |
1037 quad (@@(x) betainc (x, a, b), 0, 0.4) | |
6929 | 1038 @result{} 0.13867 |
6554 | 1039 @end example |
1040 | |
1041 @noindent | |
1042 adapts a function with several parameters to the form required by | |
1043 @code{quad}. In this example, the values of @var{a} and @var{b} that | |
1044 are passed to @code{betainc} are inherited from the current | |
1045 environment. | |
1046 | |
4933 | 1047 @node Inline Functions |
1048 @subsection Inline Functions | |
1049 | |
6638 | 1050 An inline function is created from a string containing the function |
1051 body using the @code{inline} function. The following code defines the | |
1052 function @math{f(x) = x^2 + 2}. | |
1053 | |
1054 @example | |
1055 f = inline("x^2 + 2"); | |
1056 @end example | |
1057 | |
1058 @noindent | |
1059 After this it is possible to evaluate @math{f} at any @math{x} by | |
1060 writing @code{f(x)}. | |
1061 | |
4933 | 1062 @DOCSTRING(inline) |
1063 | |
1064 @DOCSTRING(argnames) | |
1065 | |
1066 @DOCSTRING(formula) | |
1067 | |
1068 @DOCSTRING(vectorize) | |
1069 | |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
1070 @DOCSTRING(symvar) |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
1071 |
6549 | 1072 @node Commands |
1073 @section Commands | |
1074 | |
6638 | 1075 Commands are a special class of functions that only accept string |
1076 input arguments. A command can be called as an ordinary function, but | |
1077 it can also be called without the parentheses like the following example | |
1078 shows | |
1079 | |
1080 @example | |
1081 my_command hello world | |
1082 @end example | |
1083 | |
1084 @noindent | |
1085 which is the same as | |
1086 | |
1087 @example | |
1088 my_command("hello", "world") | |
1089 @end example | |
1090 | |
1091 The general form of a command call is | |
1092 | |
1093 @example | |
1094 @var{name} @var{arg1} @var{arg2} @dots{} | |
1095 @end example | |
1096 | |
1097 @noindent | |
1098 which translates directly to | |
1099 | |
1100 @example | |
1101 @var{name} ("@var{arg1}", "@var{arg2}", @dots{}) | |
1102 @end example | |
1103 | |
7001 | 1104 A function can be used as a command if it accepts string input arguments. |
6638 | 1105 To do this, the function must be marked as a command, which can be done |
1106 with the @code{mark_as_command} command like this | |
1107 | |
1108 @example | |
1109 mark_as_command name | |
1110 @end example | |
1111 | |
1112 @noindent | |
1113 where @code{name} is the function to be marked as a command. | |
1114 | |
1115 One difficulty of commands occurs when one of the string input arguments | |
1116 are stored in a variable. Since Octave can't tell the difference between | |
1117 a variable name, and an ordinary string, it is not possible to pass a | |
1118 variable as input to a command. In such a situation a command must be | |
1119 called as a function. | |
1120 | |
6549 | 1121 @DOCSTRING(mark_as_command) |
1122 | |
1123 @DOCSTRING(unmark_command) | |
1124 | |
1125 @DOCSTRING(iscommand) | |
1126 | |
1127 @DOCSTRING(mark_as_rawcommand) | |
1128 | |
1129 @DOCSTRING(unmark_rawcommand) | |
1130 | |
1131 @DOCSTRING(israwcommand) | |
1132 | |
4167 | 1133 @node Organization of Functions |
3294 | 1134 @section Organization of Functions Distributed with Octave |
1135 | |
1136 Many of Octave's standard functions are distributed as function files. | |
1137 They are loosely organized by topic, in subdirectories of | |
1138 @file{@var{octave-home}/lib/octave/@var{version}/m}, to make it easier | |
1139 to find them. | |
1140 | |
1141 The following is a list of all the function file subdirectories, and the | |
1142 types of functions you will find there. | |
1143 | |
1144 @table @file | |
1145 @item audio | |
1146 Functions for playing and recording sounds. | |
1147 | |
1148 @item control | |
1149 Functions for design and simulation of automatic control systems. | |
1150 | |
1151 @item elfun | |
1152 Elementary functions. | |
1153 | |
6554 | 1154 @item finance |
1155 Functions for computing interest payments, investment values, and rates | |
1156 of return. | |
1157 | |
3294 | 1158 @item general |
1159 Miscellaneous matrix manipulations, like @code{flipud}, @code{rot90}, | |
1160 and @code{triu}, as well as other basic functions, like | |
4029 | 1161 @code{ismatrix}, @code{nargchk}, etc. |
3294 | 1162 |
1163 @item image | |
1164 Image processing tools. These functions require the X Window System. | |
1165 | |
1166 @item io | |
1167 Input-ouput functions. | |
1168 | |
1169 @item linear-algebra | |
1170 Functions for linear algebra. | |
1171 | |
1172 @item miscellaneous | |
1173 Functions that don't really belong anywhere else. | |
1174 | |
6554 | 1175 @item optimization |
1176 Minimization of functions. | |
1177 | |
1178 @item path | |
1179 Functions to manage the directory path Octave uses to find functions. | |
1180 | |
1181 @item pkg | |
1182 Install external packages of functions in Octave. | |
1183 | |
3294 | 1184 @item plot |
6556 | 1185 Functions for displaying and printing two- and three-dimensional graphs. |
3294 | 1186 |
1187 @item polynomial | |
1188 Functions for manipulating polynomials. | |
1189 | |
1190 @item set | |
1191 Functions for creating and manipulating sets of unique values. | |
1192 | |
1193 @item signal | |
1194 Functions for signal processing applications. | |
1195 | |
6554 | 1196 @item sparse |
1197 Functions for handling sparse matrices. | |
1198 | |
3294 | 1199 @item specfun |
1200 Special functions. | |
1201 | |
1202 @item special-matrix | |
1203 Functions that create special matrix forms. | |
1204 | |
1205 @item startup | |
1206 Octave's system-wide startup file. | |
1207 | |
1208 @item statistics | |
1209 Statistical functions. | |
1210 | |
1211 @item strings | |
1212 Miscellaneous string-handling functions. | |
1213 | |
6554 | 1214 @item testfun |
1215 Perform unit tests on other functions. | |
1216 | |
3294 | 1217 @item time |
1218 Functions related to time keeping. | |
1219 @end table |