Next: , Previous: , Up: Functions and Scripts   [Contents][Index]

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.

Next: Organization of Functions Distributed with Octave, Previous: Function Handles and Anonymous Functions, Up: Functions and Scripts   [Contents][Index]