changeset 29454:e4cfe348b212 stable

doc: Expand on documentation for command syntax (bug #60084). * doc/interpreter/func.txi: Rename section "Commands" to "Command Syntax and Function Syntax". Add information about how to pass strings with spaces in command syntax. Add information about command syntax and output arguments. * doc/interpreter/octave.texi: Rename node.
author Markus Mützel <markus.muetzel@gmx.de>
date Sat, 20 Mar 2021 15:42:27 +0100
parents 766db6d094fe
children 27cb1672b249 4ae7e80fb7c5
files doc/interpreter/func.txi doc/interpreter/octave.texi
diffstat 2 files changed, 35 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/doc/interpreter/func.txi	Sat Mar 20 15:18:21 2021 +0100
+++ b/doc/interpreter/func.txi	Sat Mar 20 15:42:27 2021 +0100
@@ -41,7 +41,7 @@
 * Function Files::
 * Script Files::
 * Function Handles and Anonymous Functions::
-* Commands::
+* Command Syntax and Function Syntax::
 * Organization of Functions::
 @end menu
 
@@ -1885,12 +1885,14 @@
 @xref{Operator Overloading}, for a list of operators which also have a
 functional form.
 
-@node Commands
-@section Commands
+@node Command Syntax and Function Syntax
+@section Command Syntax and Function Syntax
+@cindex commands functions
 
-Commands are a special class of functions that only accept string
-input arguments.  A command can be called as an ordinary function, but
-it can also be called without the parentheses.  For example,
+Additionally to the function syntax described above (i.e., calling a function
+like @code{fun (arg1, arg2, @dots{})}), a function can be called using command
+syntax (for example, calling a function like @code{fun arg1 arg2 @dots{}}).  In
+that case, all arguments are passed to the function as strings.  For example,
 
 @example
 my_command hello world
@@ -1907,18 +1909,31 @@
 The general form of a command call is
 
 @example
-@var{cmdname} @var{arg1} @var{arg2} @dots{}
+cmdname arg1 arg2 @dots{}
 @end example
 
 @noindent
 which translates directly to
 
 @example
-@var{cmdname} ("@var{arg1}", "@var{arg2}", @dots{})
+cmdname ("arg1", "arg2", @dots{})
+@end example
+
+If an argument including spaces should be passed to a function in command
+syntax, (double-)quotes can be used.  For example,
+@example
+my_command "first argument" "second argument"
 @end example
 
-Any regular function can be used as a command if it accepts string input
-arguments.  For example:
+@noindent
+is equivalent to
+
+@example
+my_command ("first argument", "second argument")
+@end example
+
+Any function can be used as a command if it accepts string input arguments.
+For example:
 
 @example
 @group
@@ -1927,11 +1942,9 @@
 @end group
 @end example
 
-One difficulty of commands occurs when one of the string input arguments
-is stored in a variable.  Because Octave can't tell the difference between
-a variable name and an ordinary string, it is not possible to pass a
-variable as input to a command.  In such a situation a command must be
-called as a function.  For example:
+Since the arguments are passed as strings to the corresponding function, it is
+not possible to pass input arguments that are stored in variables.  In that
+case, a command must be called using the function syntax.  For example:
 
 @example
 @group
@@ -1943,6 +1956,12 @@
 @end group
 @end example
 
+Additionally, the return values of functions cannot be assigned to variables
+using the command syntax.  Only the first return argument is assigned to the
+built-in variable @code{ans}.  If the output argument of a command should be
+assigned to a variable, or multiple output arguments of a function should be
+returned, the function syntax must be used.
+
 
 @node Organization of Functions
 @section Organization of Functions Distributed with Octave
--- a/doc/interpreter/octave.texi	Sat Mar 20 15:18:21 2021 +0100
+++ b/doc/interpreter/octave.texi	Sat Mar 20 15:42:27 2021 +0100
@@ -413,7 +413,7 @@
 * Function Files::
 * Script Files::
 * Function Handles and Anonymous Functions::
-* Commands::
+* Command Syntax and Function Syntax::
 * Organization of Functions::
 
 Validating Arguments