Re: Passing Keyword Parameters [message #15579] |
Mon, 31 May 1999 00:00 |
davidf
Messages: 2866 Registered: September 1996
|
Senior Member |
|
|
Kristjan Onu (konu@tuli.dhs.org) writes:
> I want to write a procedure with the following prototype:
>
> PRO easy_hist, file, hist_min = hist_min_val, hist_max = hist_max_val
>
> Because I want hist_min_val and hist_max_val to be optional keyword
> parameters, I use the following in easy_hist:
>
> if (keyword_set(hist_min_val) eq 0) then hist_min_val = 1
> if (keyword_set(hist_max_val) eq 0) then hist_max_val = 255
>
> The procedure has a bug in the sense that, if it is called with
> hist_min_val = 0, keyword_set(hist_min_val) returns zero and so
> hist_min_val is set to its default value of 1. Clearly the user
> intended to set hist_min_val equal to 0.
>
> Is there a simple way fix this bug in my procedure?
This is not a bug. It is a common misunderstanding of the
purpose of the KEYWORD_SET function.
KEYWORD_SET returns a 0 if the argument is 0 or undefined.
It returns a 1 if the argument is anything else at all. It
should be used ONLY with those keywords that are going to
be set to a boolean value. That is, those keywords are are
true or false, yes or no, on or off, etc.
You should be using N_ELEMENTS to check if your keywords
are undefined or not. Your code should look like this:
if n_elements(hist_min_val) eq 0 then hist_min_val = 1
if n_elements(hist_max_val) eq 0 then hist_max_val = 255
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting
Phone: 970-221-0438 E-Mail: davidf@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|