|Date: ||Mon, 28 May 2007 22:16:26 -0700|
|Reply-To: ||David L Cassell <davidlcassell@MSN.COM>|
|Sender: ||"SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>|
|From: ||David L Cassell <davidlcassell@MSN.COM>|
|Subject: ||Re: Proc FCMP and recursion|
|Content-Type: ||text/plain; format=flowed|
>I have been using Proc FCMP to do recursion. I found that it may modify the
>variable in the caller. For instance,
>proc fcmp outlib=sasuser.funcs.math;
> function fact(k);
> if k = 1 then return(k);
> put "pre K = " k;
> z = k; *preserve k;
> x = fact(k-1);
> put "after k = " k;
> k = z; *recover k;
> k = k * x;
> x = fact(5);
> put "x = " x;
>it will not give correct answer if the two commented line are removed. Does
>anyone know why?
I have not seen anything that indicates that the functions created
in PROC FCMP will maintain their own local state independent of
other simultaneous or recursive calls to the same function.
Your code assumes that all such calls maintain their own state, as
if this really were C.
You can see the effects if you use the PUT _PDV_ statement a
few times inside your function. Note that this works in PROC FCMP
but it will *NOT* work in a data step. In a data step, you could use
PUT _ALL_ instead but the results will not be as nicely arranged.
David L. Cassell
3115 NW Norwood Pl.
Corvallis OR 97330
Like the way Microsoft Office Outlook works? You’ll love Windows Live