Recursion – Generally and in Flash

by admin on April 1, 2008

Recursion, in mathematics and computer science, is a method of defining functions in which the function being defined is applied within its own definition. The term is also used more generally to describe a process of repeating objects in a self-similar way. For instance, when the surfaces of two mirrors are almost parallel with each other the nested images that occur are a form of recursion.

Recursion is also the basic building block of fractals as their patterns re occur over and over again. So lets go through a simple recursive function.


function traceMe(a) {
//First lets check the value of the paramater and start or stop the loop based on the if statement.
if (a<10) {
//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.
trace(”This is the “+a+”th time that the function is run.”);
//We call the same exact function from within a function with the parameter(a) incremented.
traceMe(a+1);
//This is the interesting part. If you look at your output panel you will notice that after
//all the traceME functions have been executed this line of code gets executed at the end.
//There is a simple reason for that and that is because flash executes lines of code sequentially.
trace(”End of the “+a+”th function.”);
}
}
traceMe(1);

Thats about it. Simple functions for complex recursions.

Leave a Comment