Next: , Previous: Unwind-protect, Up: Octave Features


4.7 Variable-length argument lists

Octave has a real mechanism for handling functions that take an unspecified number of arguments, so it is no longer necessary to place an upper bound on the number of optional arguments that a function can accept.

Here is an example of a function that uses the new syntax to print a header followed by an unspecified number of values:

     function foo (heading, ...)
       disp (heading);
       va_start ();
       while (--nargin)
         disp (va_arg ());
       endwhile
     endfunction

Calling va_start() positions an internal pointer to the first unnamed argument and allows you to cycle through the arguments more than once. It is not necessary to call va_start() if you do not plan to cycle through the arguments more than once.

The function va_arg() returns the value of the next available argument and moves the internal pointer to the next argument. It is an error to call va_arg() when there are no more arguments available.

It is also possible to use the keyword all_va_args to pass all unnamed arguments to another function.