Prev | Current Page 196 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

is( val, Float ) || Std.is( val, Int ) )
return val + val;
else if ( Std.is( val, String ) )
return val + ??? ??? + val;
else if ( Std.is( val, Array ) )
return val.concat( val );
else
return null;
}
Recursive Functions
A recursive function is a function that calls itself. In doing so, the function creates a loop, similar to the
while and for loops, and requires that certain criteria be set in order to end the recursion. The benefits
of recursive functions are that you ultimately have more control over the looping in your code and
require only one level of functionality rather than two involved in having a loop nested in a function.
The best benefit by far with recursion, however, is that the function calls return values for each iteration.
This is a little difficult to explain without some kind of visual aid, so it is illustrated in Figure 4 - 1 .
(continued)
Chapter 4: Controlling the Flow of Information
89
Calling
function
Function
Function
Function
Function
Condition
met, now
returning
Call while
passing
values
Return value
to caller
Figure 4-1
As you can see, with normal loops, you start off with a set of values, and then as each loop occurs, the
values are modified in some way that creates a slightly different outcome from the code in the body of
the loop.


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