www.delorie.com/gnu/docs/octave/Octave-FAQ_11.html   search  
Buy GNU books!

Frequently asked questions about Octave (with answers)

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.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 ());

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.

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003