11 Replies Latest reply on Apr 20, 2006 1:58 AM by Newsgroup_User

    Recursive Functions

    Level 7
      function traceMe(n) {
      if (n<=2) {
      trace(n);
      traceMe(n+1);
      trace(n);
      }
      }
      traceMe(1);

      returns the result:

      1
      2
      2
      1

      Why is the last result 1 and not 2?

      By the time this second trace line is reached n is already equal to 2.
      How does it get reset back to 1?

      Doug
        • 1. Re: Recursive Functions
          Level 7
          Obviously you don't understand that function arguments are local to the
          function. That's something you really need to understand if you want to use
          recursion.

          lets look at what it does...

          traceMe(1) runs a copy of traceMe which has a value of 1 for n. It traces
          the value of n (which is 1), run another copy of traceMe passing the value
          of n+1 (2), then trace the value of n again (which is still 1). The value
          of n has not changed, so it should indeed trace out 1 each time.

          Now within this second copy of the traceMe, which has a value of 2 for ITS
          copy of 'n', and we do the same thing again. It traces the value of n
          (which is 2), run another copy of traceMe passing the value of n+1 (3), then
          trace the value of n again (which is still 2).

          This third copy of traceMe has its n as 3. Because n is not <= 2, we just
          return immediately without doing anything else.

          Its all perfectly sensible and correct. If it did what YOU were wanting it
          to do, it would be a massive bug (and would mean local variables were not
          local to the running of their functions at all).
          --
          Jeckyl


          • 2. Re: Recursive Functions
            Level 7
            So how can I return something from the nth iteration of the function and
            have that final result returned after all the unwinding?

            Doug
            • 3. Re: Recursive Functions
              Level 7
              return it .. and have the recursive call also return the same thing. Hard
              to tell you exactly .. show me your code and what you'd like it to do.
              --
              Jeckyl


              • 4. Re: Recursive Functions
                Level 7
                Thanks for your offer to help, but what I want to do is rather awkward.
                I think I need to have two nested recursive functions that pass a result
                backwards and forwards until the result is resolved.

                I have just got back to the problem I asked you about before. What I
                want to do is easy to write as two nested loops each requiring customer
                input, but as AS times out if the customer is a bit slow deciding, I
                think I have to use recursive functions instead. But I'm finding it
                difficult to keep track of what's in all the variables as the functions
                reiterate.

                The logic is like this:

                1. n = 0
                2. display array[n]
                3. confirm("Is this the result you want?")
                4. if yes, return array[n]
                5. if no, n += 1
                6. if n > len(array), confirm("No more options, try again or cancel?") {
                if try again, goto 1
                if cancel, return 0
                } else {
                goto 2
                }
                end

                Doug
                • 5. Re: Recursive Functions
                  Level 7
                  The logic is a fairly simple loop . .but seeing it involves interaction with
                  the user, you'll have to spread it across multiple frames (the user cannot
                  interact with the script while some code is executing like that. In any
                  case, doing it recursively is definitely a BAD idea.
                  --
                  Jeckyl



                  • 6. Re: Recursive Functions
                    Level 7
                    I cannot use frames as this is part of a flash form, so it seems to be
                    whether bad or not that recursive functions are the only option. There
                    is no other way to do it, is there?

                    Doug

                    • 7. Re: Recursive Functions
                      Level 7
                      > I cannot use frames as this is part of a flash form

                      That makes no difference .. the logic needs to be spread over a couple of
                      timeline frames to allow interaction

                      > so it seems to be whether bad or not that recursive functions are the only
                      > option.

                      Not at al .. there is NO REASONS to make it recursive. More to the point,
                      recursive have limited support in Flash .. so it is always bets to avoid
                      them.

                      > There is no other way to do it, is there?

                      There are lots of ways to do it. Recursions, however, is not one of them as
                      it is simply not possible to do what you want using recursion, or even using
                      simple loops, in Flash.
                      --
                      Jeckyl



                      • 8. Re: Recursive Functions
                        Level 7
                        > That makes no difference .. the logic needs to be spread over a couple of
                        > timeline frames to allow interaction

                        How can you create different frames from a flash form? They exist as one
                        frame created by ColdFusion which, as far as I know, cannot be altered?

                        Doug
                        • 9. Re: Recursive Functions
                          Level 7
                          > More to the point,
                          > recursive have limited support in Flash

                          I greatly appreciate all your help and warnings, but I have no choice
                          but to do this inside ColdFusion flash forms.

                          I realize that I can solve the return problem by setting the function to
                          Void and then setting a global variable instead of using return. But
                          this still leaves me with no way out of recursive functions.

                          It does seem to work, but can you be any more specific about the
                          problems that might arise from their use.

                          Or can you or anyone offer any other suggestion?

                          Doug
                          • 10. Re: Recursive Functions
                            Level 7
                            The problem is you cannot prompt for information in Flash from script.

                            There are limits to how deeply you can nest recursive function.

                            If this is stuff make by coldfusion, you should ask on a cold fusion forum
                            .. I don't use it and don't know why you cannot have more than one frame on
                            a timeline using it.

                            But even if recursion worked (which it doesn't) it would STILL be
                            inappropriate for something that is essentially just a simple loop.
                            Recursion has lots of extra overheads and limits the simple loops do not
                            have.
                            --
                            Jeckyl


                            • 11. Re: Recursive Functions
                              Level 7
                              No-one on the cf forum seems to know much about AS.

                              I can tell you that in CF flash forms prompt does work with no problems.

                              CF flash forms appear to have only one frame, but this is all hidden by
                              CF and you cannot change it.

                              The recursive function is the only way I can find to make it work, but I
                              am still testing.

                              Thanks for all your help.

                              Doug