Prev | Current Page 192 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

When your function is used, values will be passed with the function call that
represent the parameters supplied in the function prototype and will be required to be of the same type
or to extend the type of the parameter. The function can then use these values by accessing them with the
parameter name as it would any variable.
The last part of the function is the return value. Return values are the value of the data that is returned
from the function once the necessary processes are complete. If no value is returned, the type specified
must be Void .
Here ??™ s a simple function in action:
public static function main()
{
var i : Int = 0;
for ( j in 1...10 )
{
i = add( i, j );
trace( i );
}
}
public static function add( num : Int, numToAdd : Int ) : Int
{
num += numToAdd;
return num;
}
Here, the simple function add adds the value numToAdd to the value num . When the loop is first iterated,
the value of j , which is 1, is added to the variable i . On subsequent iterations, the value of j is
incremented by one before being added to the variable i , so the value i contains with each iteration is
that of a triangle number.


Pages:
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204