This is an archived tutorial from the kirupa.com legacy collection. It covers software that may no longer be available, but it is kept online because the ideas still hold up.
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.
function traceMe(n) {
if (n<=5) {
trace("This is the "+n+"th time that the function is run.");
traceMe(n+1);
trace("End of the "+n+"th function.");
}
}
traceMe(1);
[ 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) ;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} |
Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence, ads, and algorithm-driven doodads. A huge thank you to all of you who buy kirupa's books, became a paid subscriber, watch the videos, and/or interact on the forums.
Your support keeps this site going! 😇
:: Copyright KIRUPA 2026 //--