Re: Recursive function/procedure [message #15133] |
Sat, 24 April 1999 00:00 |
Brian Jackel
Messages: 34 Registered: January 1998
|
Member |
|
|
Tri VU KHAC wrote:
>
> Hi folks,
>
> I did not see anyone tells about this problem:
>
> function AAA, a,b,C=c,D=d
> return, AAA(a, b, C=c, D=d)
> end
>
> Creating this function is very stupid. But the problem is not in this
> function.
> Writing this function is correct, I think. But if you compile this, the
> compilator says there is a syntax error:
>
> return, AAA(a, b, C=c, D=d)
> ^
> Syntax error.
>
> However, if I do a litle strick:
> +remove the two last parameters (c,d)
> +compile this function (no error here)
> +add the two last parameters (c,d)
> +re-compile this function - well, it passes with no error.
>
> Now this function already works very well.
> Could you tell me why ? How to avoid this stupid doing ?
> Best regards,
Take a look at the FORWARD_FUNCTION command.
--
Brian Jackel
|
|
|
Re: Recursive function/procedure [message #15136 is a reply to message #15133] |
Sat, 24 April 1999 00:00  |
VU KHAC Tri
Messages: 25 Registered: March 1999
|
Junior Member |
|
|
Hi folks,
I did not see anyone tells about this problem:
function AAA, a,b,C=c,D=d
return, AAA(a, b, C=c, D=d)
end
Creating this function is very stupid. But the problem is not in this
function.
Writing this function is correct, I think. But if you compile this, the
compilator says there is a syntax error:
return, AAA(a, b, C=c, D=d)
^
Syntax error.
However, if I do a litle strick:
+remove the two last parameters (c,d)
+compile this function (no error here)
+add the two last parameters (c,d)
+re-compile this function - well, it passes with no error.
Now this function already works very well.
Could you tell me why ? How to avoid this stupid doing ?
Best regards,
Tri.
Tri VU KHAC wrote:
> Hi folks,
> How to write a recursive function/procedure ?
> What are programming problems of this technique in IDL ?
> Thanks for help.
> Best regards,
> Tri
|
|
|
Re: Recursive function/procedure [message #15150 is a reply to message #15133] |
Fri, 23 April 1999 00:00  |
Martin Schultz
Messages: 515 Registered: August 1997
|
Senior Member |
|
|
Tri VU KHAC wrote:
>
> Hi folks,
> How to write a recursive function/procedure ?
> What are programming problems of this technique in IDL ?
> Thanks for help.
> Best regards,
> Tri
since the first question has been answered let me address the second
one:
as always with recursive procedures, you must take care not to run into
infinite loops (and there is some limitation as to how many recursions
you can make although I don't know the number now). Hence, a "safe"
recursive procedure would look like this:
pro recurse,level
MAX_LEVEL = 1000 ; or whatever is reasonable
if (n_elements(level) eq 0) then level = 0 ; main entry level
if (level gt MAX_LEVEL) then begin
message,'Maximum number of iterations exceeded!' ; optional +
,/Continue
return
endif
; *** do your stuff here
recurse,level+1
; *** do more stuff here
return
end
Regards,
Martin.
--
------------------------------------------------------------ -------
Dr. Martin Schultz
Department for Engineering&Applied Sciences, Harvard University
109 Pierce Hall, 29 Oxford St., Cambridge, MA-02138, USA
phone: (617)-496-8318
fax : (617)-495-4551
e-mail: mgs@io.harvard.edu
Internet-homepage: http://www-as.harvard.edu/people/staff/mgs/
------------------------------------------------------------ -------
|
|
|
Re: Recursive function/procedure [message #15157 is a reply to message #15150] |
Fri, 23 April 1999 00:00  |
ronn
Messages: 123 Registered: April 1999
|
Senior Member |
|
|
In article <372050FC.85669B9@info.fundp.ac.be>,
Tri VU KHAC <tvk@info.fundp.ac.be> wrote:
> Hi folks,
> How to write a recursive function/procedure ?
> What are programming problems of this technique in IDL ?
> Thanks for help.
> Best regards,
> Tri
>
>
Tri,
I have a couple of sections of using recursion (procedures, functions and
methods!) in my new book. Check out my web site at www.rlkling.com for
details.
-Ronn
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
|
|
|
|
Re: Recursive function/procedure [message #15164 is a reply to message #15150] |
Fri, 23 April 1999 00:00  |
wbiagiot
Messages: 59 Registered: January 1999
|
Member |
|
|
Tri VU KHAC <tvk@info.fundp.ac.be> wrote:
> Hi folks,
> How to write a recursive function/procedure ?
> What are programming problems of this technique in IDL ?
> Thanks for help.
> Best regards,
> Tri
>
>
Hi Tri,
Well, writing a recursive routine is pretty much the same in any language.
Here's a quick example:
PRO My_routine, My_mode_parameter
CASE (My_mode_parameter) OF
0: BEGIN ; Perform all 3 actions
My_routine, 1
My_routine, 2
My_routine, 3
END
1: BEGIN
<action 1>
END
2: BEGIN
<action 2>
END
3: BEGIN
<action 3>
END
ELSE:
ENDCASE
END ; PRO
Hope this helps.
-Bill
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
|
|
|