dealing with arguments [message #71119] |
Wed, 02 June 2010 14:59  |
fgg
Messages: 67 Registered: April 2010
|
Member |
|
|
Hello,
In this call:
proname, mode, s1, s2
s1 is necessary if mode=1; s2 is necessary if mode=2; and both s1 and
s2 are necessary if mode=3.
If mode=1, s2 can be simply omitted, but if mode=2 then the user would
still need to provide a s1, even though it will not be used, just
because 3 numbers are needed in the calling sequence in order to
define s2. Is there a way to avoid entering s1 when mode=2 while still
making IDL understands that the second number in the call refers to
s2? I hope this makes sense. Thank you!
|
|
|
Re: dealing with arguments [message #71262 is a reply to message #71119] |
Wed, 02 June 2010 15:52  |
fgg
Messages: 67 Registered: April 2010
|
Member |
|
|
On Jun 2, 3:43 pm, pp <pp.pente...@gmail.com> wrote:
>> Oh! And how do I set default values? For example, to make s2 always
>> equal 10 if no other value is provided in the call.
>
> pro name, mode, s1=s1, s2=s2
> s2=n_elements(s2) ne 0 ? s2 : 10
> ...
> end
>
> Which you could then call as
>
> name, 2, s2=100
>
> It would make s2=10 if it is not provided (or is given an undefined
> variable).
>
> By the way, it could also be called with
>
> name, s2=100, 2
>
> as it does not matter in which order you supply the keywords.
Thanks a lot for your help.
|
|
|
Re: dealing with arguments [message #71263 is a reply to message #71119] |
Wed, 02 June 2010 15:50  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
pp writes:
> pro name, mode, s1=s1, s2=s2
> s2=n_elements(s2) ne 0 ? s2 : 10
> ...
> end
>
> Which you could then call as
>
> name, 2, s2=100
>
> It would make s2=10 if it is not provided (or is given an undefined
> variable).
An alternative, which is a bit more descriptive in
your programs, is SetDefaultValue:
pro name, mode, s1=s1, s2=s2
setdefaultvalue, s1, 5
setdefaultvalue, s2, 10
...
The default value is set (using pp's notation, basically)
only if the variable is undefined when the program is run.
You can find the program here:
http://www.dfanning.com/programs/setdefaultvalue.pro
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thue. ("Perhaps thos speakest truth.")
|
|
|
Re: dealing with arguments [message #71265 is a reply to message #71119] |
Wed, 02 June 2010 15:43  |
penteado
Messages: 866 Registered: February 2018
|
Senior Member Administrator |
|
|
> Oh! And how do I set default values? For example, to make s2 always
> equal 10 if no other value is provided in the call.
pro name, mode, s1=s1, s2=s2
s2=n_elements(s2) ne 0 ? s2 : 10
...
end
Which you could then call as
name, 2, s2=100
It would make s2=10 if it is not provided (or is given an undefined
variable).
By the way, it could also be called with
name, s2=100, 2
as it does not matter in which order you supply the keywords.
|
|
|
Re: dealing with arguments [message #71266 is a reply to message #71119] |
Wed, 02 June 2010 15:22  |
fgg
Messages: 67 Registered: April 2010
|
Member |
|
|
On Jun 2, 3:17 pm, fgg <fabioguimaraesgoncal...@gmail.com> wrote:
> On Jun 2, 3:12 pm, pp <pp.pente...@gmail.com> wrote:
>
>> Use keywords instead of positional arguments:
>
>> pro name, mode, s1=s1, s2=s2
>
>> That way, when calling the routine you can use any order, or omit any
>> argument, and they still get associated to the right variables. Also
>> consider replacing s1 and s2 by more descriptive names, to make it
>> easier for users to know what the arguments mean. With keywords, you
>> can still use the short names inside your routine:
>
>> pro name, mode, some_argument_name_1=s1, some_other_argument=s2
>
> Thank you, pp. So if mode=2 and I want to make s2=100 then all I need
> is:
>
> proname, 2, s2=100 ?
Oh! And how do I set default values? For example, to make s2 always
equal 10 if no other value is provided in the call.
|
|
|