Recursion in Flash
      
by ilyas usal

We call recursion the fact of allowing a function to call itself until some condition is met. It is really useful when you're doing complicated math calculations AND when you're trying to draw repetitive patters in Flash. Now I know you guys would love to hear about the math, but life's unfair, I'll only talk about the Flash.

All right, so a recursive function is a function that does stuff, and then calls itself, possibly with a different set of parameter, until we tell it to stop. To illustrate this with a simple example, we are going to create a function that takes an integer as a parameter, outputs it and then calls itself with the next integer, until 5 is reached.

[ copy and paste the above code into your actions window ]

Running the above code by pressing Ctrl + Enter will produce the following output:

This is the 1th time that the function is run.
This is the 2th time that the function is run.
This is the 3th time that the function is run.
This is the 4th time that the function is run.
This is the 5th time that the function is run.
End of the 5th function.
End of the 4th function.
End of the 3th function.
End of the 2th function.
End of the 1th function.

Let's have a look at how the function works in detail:

  • if ( n <= 5 )
    First, we check the value of the parameter. If it is greater than 5, the function does nothing at all, which means that we stop the loop. If it's not, we step into it.
     
  • trace ( "This is the " + n + "th time that the function is run." )
    We output the current value of the parameter. Therefore, the first time that piece of code is executed, Flash outputs 1th, because it's the firth time we step into the loop. Then it will be the second, the third, etc.
     
  • traceMe ( n + 1 )
    We call that same exact function from within the function, with the parameter incremented. So the firth time the code is executed, we call traceMe ( 2 ).
     
  • trace ( "End of the " + n + "th function." )
    Now if you've paid real good attention to what happened, you'll notice that this line of code gets executed when all the traceMe functions have been fully completed. That's because Flash executes lines of code sequentially, so it has to wait for the line traceMe (2) ;
    to be over before it can execute the next line, but inside that traceMe line, we call traceMe (3) so it has to wait again and so on and so forth until we reach 5.

That, my friends, is what is called recursion.

Dangers of Recursion
I'm sure you can all see the danger here: if no condition is set to stop the loop, Flash will call the function over and over and over (and over) within the same frame. So if you feel like crashing your computer (well, not your computer, but at least your Flash movie...), just remove the if (...) line (and the corresponding bracket, of course). By doing that you create an infinite loop. That's why it is very important to set a condition to stop the loop from going on forever.

There are more tutorials similar to this, so be sure to check out the ones that follow this tutorial such as fractals, etc.

Ilyas Usal
{Pictures 1 | 2}

 



SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.