Persistent Variables during recursion

Alex Tweedly alex at tweedly.net
Thu Jan 26 15:51:23 EST 2012


Easiest way I know is to have
  - your variables be either global or script-local
  - add an additional parameter, used only for recursive calls.

so for the public interface, used from anywhere else, you do something like

     put myFunction(a,b,c) into myVar

but within the function itself, you do something like

> function myFunction pA, pB, pC, pRecursive
>   if pRecursive is empty or pRecursive is false then
>     put empty into sVar1
>     put empty into gVar2
>   end if
>
>    put myFunction(mya, myb, myc, TRUE) into myVar
>
> end myFunction
>
Of course, you can do more interesting things like
>    if pRecursive is empty then
>       put 0 into pRecursive
>    end if
>    put myFunction(a,b,c,pRecursive+1) into mVar
and then you also know how deeply you are nested (perhaps to give a more 
graceful recovery than "recursion limit exceeded" if something is going 
wrong ...)

-- Alex.

On 26/01/2012 20:04, Bob Sneidar wrote:
> Hi all. This is my first foray into recursive functions. There are two variables I would like to be persistent for as long as the script is running, but I want them to be empty the first time I call the recursive function. I have tried declaring the variables as local, but each time the function calls itself, the variables are wiped as though a new set is made. If I declare them as global, then of course the value persists even after the last recursion terminates. The function has no idea if it is  the first time through or one of the iterations. Any idea how to get around this?
>
> Bob
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
> .
>





More information about the use-livecode mailing list