comparison doc/interpreter/func.txi @ 6556:8810bbf321ce

[project @ 2007-04-20 18:39:40 by jwe]
author jwe
date Fri, 20 Apr 2007 18:39:41 +0000
parents 5dde4dc2bcaf
children e0e1c6df5ab2
comparison
equal deleted inserted replaced
6555:69e864d21c11 6556:8810bbf321ce
539 539
540 @node Function Files 540 @node Function Files
541 @section Function Files 541 @section Function Files
542 @cindex function file 542 @cindex function file
543 543
544 @c FIXME need discussion of subfunctions here
545
544 Except for simple one-shot programs, it is not practical to have to 546 Except for simple one-shot programs, it is not practical to have to
545 define all the functions you need each time you need them. Instead, you 547 define all the functions you need each time you need them. Instead, you
546 will normally want to save them in a file so that you can easily edit 548 will normally want to save them in a file so that you can easily edit
547 them, and save them for use at a later time. 549 them, and save them for use at a later time.
548 550
551 place where Octave can find them. 553 place where Octave can find them.
552 554
553 When Octave encounters an identifier that is undefined, it first looks 555 When Octave encounters an identifier that is undefined, it first looks
554 for variables or functions that are already compiled and currently 556 for variables or functions that are already compiled and currently
555 listed in its symbol table. If it fails to find a definition there, it 557 listed in its symbol table. If it fails to find a definition there, it
556 searches a list of directories (the @deffn{path}) for files ending in 558 searches a list of directories (the @dfn{path}) for files ending in
557 @file{.m} that have the same base name as the undefined 559 @file{.m} that have the same base name as the undefined
558 identifier.@footnote{The @samp{.m} suffix was chosen for compatibility 560 identifier.@footnote{The @samp{.m} suffix was chosen for compatibility
559 with @sc{Matlab}.} Once Octave finds a file with a name that matches, 561 with @sc{Matlab}.} Once Octave finds a file with a name that matches,
560 the contents of the file are read. If it defines a @emph{single} 562 the contents of the file are read. If it defines a @emph{single}
561 function, it is compiled and executed. @xref{Script Files}, for more 563 function, it is compiled and executed. @xref{Script Files}, for more
621 @DOCSTRING(autoload) 623 @DOCSTRING(autoload)
622 624
623 @DOCSTRING(builtin) 625 @DOCSTRING(builtin)
624 626
625 @DOCSTRING(dispatch) 627 @DOCSTRING(dispatch)
628
629 @menu
630 * Subfunctions::
631 @end menu
632
633 @node Subfunctions
634 @subsection Subfunctions
635
636 A function file may contain secondary functions called
637 @dfn{subfunctions}. These secondary functions are only visible to the
638 other functions in the same function file. For example, a file
639 @file{f.m} containing
640
641 @example
642 @group
643 function f ()
644 printf ("in f, calling g\n");
645 g ()
646 endfunction
647 function g ()
648 printf ("in g, calling h\n");
649 endfunction
650 function h ()
651 printf ("in h\n")
652 endfunction
653 @end group
654 @end example
655
656 @noindent
657 defines a main function @code{f} and two subfunctions. The
658 subfunctions @code{g} and @code{h} may only be called from the main
659 function @code{f} or from the other subfunctions, but not from outside
660 the file @file{f.m}.
626 661
627 @node Script Files 662 @node Script Files
628 @section Script Files 663 @section Script Files
629 664
630 A script file is a file containing (almost) any sequence of Octave 665 A script file is a file containing (almost) any sequence of Octave
953 988
954 @noindent 989 @noindent
955 For example 990 For example
956 991
957 @example 992 @example
958 f = @sin; 993 f = @@sin;
959 @end example 994 @end example
960 995
961 @noindent 996 @noindent
962 Creates a function handle called @code{f} that refers to the the 997 Creates a function handle called @code{f} that refers to the the
963 function @code{sin}. 998 function @code{sin}.
965 Function handles are used to call other functions indirectly, or to pass 1000 Function handles are used to call other functions indirectly, or to pass
966 a function as an argument to another function like @code{quad} or 1001 a function as an argument to another function like @code{quad} or
967 @code{fsolve}. For example 1002 @code{fsolve}. For example
968 1003
969 @example 1004 @example
970 f = @sin; 1005 f = @@sin;
971 quad (f, 0, pi) 1006 quad (f, 0, pi)
972 @result 1.8391 1007 @result 1.8391
973 @end example 1008 @end example
974 1009
975 You may use @code{feval} to call a function using function handle, or 1010 You may use @code{feval} to call a function using function handle, or
976 simply write the name of the function handle follwed by an argument 1011 simply write the name of the function handle follwed by an argument
977 list. If there are no arguments, you must use an empty argument list 1012 list. If there are no arguments, you must use an empty argument list
978 @samp{()}. For example 1013 @samp{()}. For example
979 1014
980 @example 1015 @example
981 f = @sin; 1016 f = @@sin;
982 feval (f, pi/4) 1017 feval (f, pi/4)
983 @result 0.70711 1018 @result 0.70711
984 f (pi/4) 1019 f (pi/4)
985 @result 0.70711 1020 @result 0.70711
986 @end example 1021 @end example
1114 1149
1115 @item pkg 1150 @item pkg
1116 Install external packages of functions in Octave. 1151 Install external packages of functions in Octave.
1117 1152
1118 @item plot 1153 @item plot
1119 A set of functions that implement the @sc{Matlab}-like plotting functions. 1154 Functions for displaying and printing two- and three-dimensional graphs.
1120 1155
1121 @item polynomial 1156 @item polynomial
1122 Functions for manipulating polynomials. 1157 Functions for manipulating polynomials.
1123 1158
1124 @item set 1159 @item set