Re: How to judge a Function's parameters ? [message #66271] |
Thu, 07 May 2009 15:02  |
Michael Galloy
Messages: 1114 Registered: April 2006
|
Senior Member |
|
|
Hu wrote:
> Hi,folks,
>
> I created a function 'RectArea' to calculate the rectangle area, and
> two parameters, side a and b, are needed when the function is
> employed.
>
> I want to do something like this: if a and b are omitted by the user,
> then the function will use the default values.
>
> But, How can I judge that the parameters are omitted?
>
> Thanks
>
> function RectArea,a,b
> return,a*b
> end
Use N_ELEMENTS to determine if a particular parameter is omitted. I
usually do something like the following:
function RectArea, a, b
compile_opt strictarr
_a = n_elements(a) eq 0L ? 1.0 : a
_b = n_elements(a) eq 0L ? 1.0 : b
return, _a * _b
end
I don't do:
if (n_elements(a) eq 0L) then a = 1.0
because I don't want change an input variable from the user.
Mike
--
www.michaelgalloy.com
Associate Research Scientist
Tech-X Corporation
|
|
|
Re: How to judge a Function's parameters ? [message #66272 is a reply to message #66271] |
Thu, 07 May 2009 15:01   |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
Hu wrote:
> Hi,folks,
>
> I created a function 'RectArea' to calculate the rectangle area, and
> two parameters, side a and b, are needed when the function is
> employed.
>
> I want to do something like this: if a and b are omitted by the user,
> then the function will use the default values.
>
> But, How can I judge that the parameters are omitted?
>
> Thanks
>
> function RectArea,a,b
> return,a*b
> end
function RectArea,a,b
if ( n_elements(a) eq 0 ) then a = 1 ; or whatever default value
if ( n_elements(b) eq 0 ) then b = 1 ; or whatever default value
return,a*b
end
Although, I would rather an error be thrown if required arguments are missing.
cheers,
paulv
|
|
|
Re: How to judge a Function's parameters ? [message #66470 is a reply to message #66271] |
Thu, 07 May 2009 15:32  |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
mgalloy wrote:
> Hu wrote:
>> Hi,folks,
>>
>> I created a function 'RectArea' to calculate the rectangle area, and
>> two parameters, side a and b, are needed when the function is
>> employed.
>>
>> I want to do something like this: if a and b are omitted by the user,
>> then the function will use the default values.
>>
>> But, How can I judge that the parameters are omitted?
>>
>> Thanks
>>
>> function RectArea,a,b
>> return,a*b
>> end
>
> Use N_ELEMENTS to determine if a particular parameter is omitted. I
> usually do something like the following:
>
> function RectArea, a, b
> compile_opt strictarr
>
> _a = n_elements(a) eq 0L ? 1.0 : a
> _b = n_elements(a) eq 0L ? 1.0 : b
>
> return, _a * _b
> end
>
> I don't do:
>
> if (n_elements(a) eq 0L) then a = 1.0
>
> because I don't want change an input variable from the user.
Excellent point.
Being a Fortran programmer, I rely on the INTENT(IN) attribute for input-only dummy
arguments in my Fortran code. I'm much lazier in my IDL dabblings.
To the OP: do what Mike says. :o)
cheers,
paulv
|
|
|