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

Home » Public Forums » archive » writing recursive functions
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: writing recursive functions [message #15649 is a reply to message #15645] Wed, 02 June 1999 00:00 Go to previous message
davidf is currently offline  davidf
Messages: 2866
Registered: September 1996
Senior Member
Martin Downing (m.downing@abdn.ac.uk) writes:

> My aim is to write a function that (recursively) call itself, that can be
> compiled on the fly as with standard code.
> So - whats the problem, well if you _could_ write a function for factorials
> using recursion as:
>
> ------------------------------------------------------------
> ; fact.pro : take 1
> ; example of recursion (I know there's a factorial function!!)
> function fact , n=n
> res = float(n)
> if res gt 1 then $
> res = res * fact(n=res-1)
> return, res
> end
> ; (I used a keyword as otherwise it compiles thinking fact() is a variable)
> ------------------------------------------------------------
>
> ...This will not compile

How about this, using Forward_Function to indicate
that FACT is a function and not a variable. You could
go back to using n as a positional parameter. :-)

function fact , n=n
forward_function fact
res = float(n)
if res gt 1 then $
res = res * fact(n=res-1)
return, res
end

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
[Message index]
 
Read Message
Read Message
Read Message
Previous Topic: IDL interface to Oracle
Next Topic: Redhat Linux 6.0 troubles continue

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

Current Time: Fri Oct 10 06:10:28 PDT 2025

Total time taken to generate the page: 0.31952 seconds