11.13 Command Syntax and Function Syntax

Additionally to the function syntax described above (i.e., calling a function like fun (arg1, arg2, …)), a function can be called using command syntax (for example, calling a function like fun arg1 arg2 …). In that case, all arguments are passed to the function as strings. For example,

my_command hello world

is equivalent to

my_command ("hello", "world")

The general form of a command call is

cmdname arg1 arg2 …

which translates directly to

cmdname ("arg1", "arg2", …)

If an argument including spaces should be passed to a function in command syntax, (double-)quotes can be used. For example,

my_command "first argument" "second argument"

is equivalent to

my_command ("first argument", "second argument")

Any function can be used as a command if it accepts string input arguments. For example:

toupper lower_case_arg
   ⇒ ans = LOWER_CASE_ARG

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:

strvar = "hello world";
toupper strvar
   ⇒ ans = STRVAR
toupper (strvar)
   ⇒ ans = HELLO WORLD

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 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.

© 1996–2020 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/v6.3.0/Command-Syntax-and-Function-Syntax.html