octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

default parameter values (was: Re: The contrast function)


From: John W. Eaton
Subject: default parameter values (was: Re: The contrast function)
Date: Tue, 25 Mar 2008 14:47:26 -0400

Looking at the new contrast function, it could also be written like
this:

  function map = contrast (x, n = rows (colormap ()))

    if (nargin == 1 || nargin == 2)
      if (! isscalar (n))
        error ("contrast: n must be a scalar");
      endif

      [... code to do real work ...]

    else
      print_usage ();
    endif

  endfunction

I had to fix a small bug that was preventing the function call in the
initializer from working correctly, but assuming that works, what do
people think about using this style when possible?  Note that nargin
still refers to the number of arguments supplied when the function is
called, but the parameter N is always defined, either by the caller or
from the initializer experession.

I'd like to know whether people think that using this style would be
an improvement (and so we should encourage it), or whether it would be
a possible source of confusion and we should avoid it (and perhaps
remove the feature from Octave given the possibility of future
compatibility issues).

We can always replace code like the above with

  function map = contrast (x, n)

    if (nargin < 2)
      n = colormap ();
    endif

    if (nargin == 1 || nargin == 2)
      ...

so it is not a tremendous loss to not have this feature.

If we do decide that this feature is useful and should be encouraged,
then I think there are quite a few functions in Octave that could be
changed to use it.

Thanks,

jwe


reply via email to

[Prev in Thread] Current Thread [Next in Thread]