Prev | Current Page 197 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

You can mimic this with recursive functions, also, but the real power comes from the values they
return. Each call to the function is handled by a previous call of the function, so the calls are, in effect,
nested. While the recursion takes place, the point of execution buries itself into each new function call. Then,
when the recursion eventually ends, the point of execution comes back out of each function call returning a
value with it. This forms the effect of looping in one direction, then reversing the loop the next.
Let ??™ s see this in a couple of examples. First you can mimic the effect of a while loop with the following:
public static function main()
{
var i = 0;
i = loop( i );
trace( i ); // Outputs: 20
}
public static function loop( num : Int ) : Int
{
return if ( num < 20 )
{
// do code
loop( num + 1 );
} else num;
}
When executed, the loop function performs in a similar way to a while loop with the exception that the
loop function returns the value representing the number of iterations that have taken place.


Pages:
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209