Using the Rest Argument (...): represents a variable number of arguments as an array followed by a name for that array
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
trace(sum(1,2,3,4,5)); // Displays: 15
}
function sum(... numbers):Number {
var result:Number = 0;
for each (var num:Number in numbers) {
result += num;
}
return result;
}
}
}
Related examples in the same category