Mercurial > octave
comparison doc/interpreter/func.txi @ 19594:fe689210525c gui-release
maint: Periodic merge of stable to gui-release.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 20 Jan 2015 10:05:42 -0500 |
parents | 4cf930a64fad 446c46af4b42 |
children | 0e1f5a750d00 |
comparison
equal
deleted
inserted
replaced
19590:6d75f1683ce8 | 19594:fe689210525c |
---|---|
4 @c | 4 @c |
5 @c Octave is free software; you can redistribute it and/or modify it | 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 | 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 | 7 @c Free Software Foundation; either version 3 of the License, or (at |
8 @c your option) any later version. | 8 @c your option) any later version. |
9 @c | 9 @c |
10 @c Octave is distributed in the hope that it will be useful, but WITHOUT | 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 | 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 | 12 @c FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
13 @c for more details. | 13 @c for more details. |
14 @c | 14 @c |
15 @c You should have received a copy of the GNU General Public License | 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 | 16 @c along with Octave; see the file COPYING. If not, see |
17 @c <http://www.gnu.org/licenses/>. | 17 @c <http://www.gnu.org/licenses/>. |
18 | 18 |
19 @node Functions and Scripts | 19 @node Functions and Scripts |
468 | 468 |
469 @noindent | 469 @noindent |
470 This implementation handles any number of input arguments, but it's also | 470 This implementation handles any number of input arguments, but it's also |
471 a very simple solution to the problem. | 471 a very simple solution to the problem. |
472 | 472 |
473 A slightly more complex example of @code{varargin} is a function | 473 A slightly more complex example of @code{varargin} is a function |
474 @code{print_arguments} that prints all input arguments. Such a function | 474 @code{print_arguments} that prints all input arguments. Such a function |
475 can be defined like this | 475 can be defined like this |
476 | 476 |
477 @example | 477 @example |
478 @group | 478 @group |
777 addpath ("~/Octave") | 777 addpath ("~/Octave") |
778 @end example | 778 @end example |
779 | 779 |
780 @noindent | 780 @noindent |
781 After this the directory @samp{~/Octave} will be searched for functions. | 781 After this the directory @samp{~/Octave} will be searched for functions. |
782 | 782 |
783 @DOCSTRING(addpath) | 783 @DOCSTRING(addpath) |
784 | 784 |
785 @DOCSTRING(genpath) | 785 @DOCSTRING(genpath) |
786 | 786 |
787 @DOCSTRING(rmpath) | 787 @DOCSTRING(rmpath) |
856 @end group | 856 @end group |
857 @end example | 857 @end example |
858 | 858 |
859 @noindent | 859 @noindent |
860 Then if the path to @code{func1} is @code{<directory>/func1.m}, and if | 860 Then if the path to @code{func1} is @code{<directory>/func1.m}, and if |
861 @code{func2} is found in the directory @code{<directory>/private/func2.m}, | 861 @code{func2} is found in the directory @code{<directory>/private/func2.m}, |
862 then @code{func2} is only available for use of the functions, like | 862 then @code{func2} is only available for use of the functions, like |
863 @code{func1}, that are found in @code{<directory>}. | 863 @code{func1}, that are found in @code{<directory>}. |
864 | 864 |
865 @node Nested Functions | 865 @node Nested Functions |
866 @subsection Nested Functions | 866 @subsection Nested Functions |
867 | 867 |
1017 @subsection Overloading and Autoloading | 1017 @subsection Overloading and Autoloading |
1018 | 1018 |
1019 Functions can be overloaded to work with different input arguments. For | 1019 Functions can be overloaded to work with different input arguments. For |
1020 example, the operator '+' has been overloaded in Octave to work with single, | 1020 example, the operator '+' has been overloaded in Octave to work with single, |
1021 double, uint8, int32, and many other arguments. The preferred way to overload | 1021 double, uint8, int32, and many other arguments. The preferred way to overload |
1022 functions is through classes and object oriented programming | 1022 functions is through classes and object oriented programming |
1023 (@pxref{Function Overloading}). Occasionally, however, one needs to undo | 1023 (@pxref{Function Overloading}). Occasionally, however, one needs to undo |
1024 user overloading and call the default function associated with a specific | 1024 user overloading and call the default function associated with a specific |
1025 type. The @code{builtin} function exists for this purpose. | 1025 type. The @code{builtin} function exists for this purpose. |
1026 | 1026 |
1027 @DOCSTRING(builtin) | 1027 @DOCSTRING(builtin) |
1120 @enumerate 1 | 1120 @enumerate 1 |
1121 @item Subfunction | 1121 @item Subfunction |
1122 A subfunction with the required function name in the given scope. | 1122 A subfunction with the required function name in the given scope. |
1123 | 1123 |
1124 @item Private function | 1124 @item Private function |
1125 A function defined within a private directory of the directory | 1125 A function defined within a private directory of the directory |
1126 which contains the current function. | 1126 which contains the current function. |
1127 | 1127 |
1128 @item Class constructor | 1128 @item Class constructor |
1129 A function that constuctors a user class as defined in chapter | 1129 A function that constuctors a user class as defined in chapter |
1130 @ref{Object Oriented Programming}. | 1130 @ref{Object Oriented Programming}. |
1131 | 1131 |
1132 @item Class method | 1132 @item Class method |
1133 An overloaded function of a class as in chapter | 1133 An overloaded function of a class as in chapter |
1134 @ref{Object Oriented Programming}. | 1134 @ref{Object Oriented Programming}. |
1166 named in a script file are not local variables, but are in the same | 1166 named in a script file are not local variables, but are in the same |
1167 scope as the other variables that are visible on the command line. | 1167 scope as the other variables that are visible on the command line. |
1168 | 1168 |
1169 Even though a script file may not begin with the @code{function} | 1169 Even though a script file may not begin with the @code{function} |
1170 keyword, it is possible to define more than one function in a single | 1170 keyword, it is possible to define more than one function in a single |
1171 script file and load (but not execute) all of them at once. To do | 1171 script file and load (but not execute) all of them at once. To do |
1172 this, the first token in the file (ignoring comments and other white | 1172 this, the first token in the file (ignoring comments and other white |
1173 space) must be something other than @code{function}. If you have no | 1173 space) must be something other than @code{function}. If you have no |
1174 other statements to evaluate, you can use a statement that has no | 1174 other statements to evaluate, you can use a statement that has no |
1175 effect, like this: | 1175 effect, like this: |
1176 | 1176 |
1259 @cindex anonymous functions | 1259 @cindex anonymous functions |
1260 @cindex inline, inline functions | 1260 @cindex inline, inline functions |
1261 | 1261 |
1262 It can be very convenient store a function in a variable so that it | 1262 It can be very convenient store a function in a variable so that it |
1263 can be passed to a different function. For example, a function that | 1263 can be passed to a different function. For example, a function that |
1264 performs numerical minimization needs access to the function that | 1264 performs numerical minimization needs access to the function that |
1265 should be minimized. | 1265 should be minimized. |
1266 | 1266 |
1267 @menu | 1267 @menu |
1268 * Function Handles:: | 1268 * Function Handles:: |
1269 * Anonymous Functions:: | 1269 * Anonymous Functions:: |
1412 @example | 1412 @example |
1413 my_command hello world | 1413 my_command hello world |
1414 @end example | 1414 @end example |
1415 | 1415 |
1416 @noindent | 1416 @noindent |
1417 is equivalent to | 1417 is equivalent to |
1418 | 1418 |
1419 @example | 1419 @example |
1420 my_command ("hello", "world") | 1420 my_command ("hello", "world") |
1421 @end example | 1421 @end example |
1422 | 1422 |