comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: How to judge a Function's parameters ?
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: How to judge a Function's parameters ? [message #66271] Thu, 07 May 2009 15:02 Go to next message
Michael Galloy is currently offline  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 Go to previous messageGo to next message
Paul Van Delst[1] is currently offline  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 Go to previous message
Paul Van Delst[1] is currently offline  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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: How to judge a Function's parameters ?
Next Topic: IDL 7.0 (Windows version) - widget_table does not generate expected event

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 19:24:55 PDT 2025

Total time taken to generate the page: 0.00594 seconds