Functions
Functions in flash can be defined in two ways. First you can create a function using :
function [function name]([parameters]){[codes]}
and second way, you can also write :
var [function name] = function([parameters]){[codes]}
Both way with give you the same result. But by using variable defining (the second way), you can only define the function once in one timeline.
For better debuging, it is highly recommended to assign type to the functions. For example :
function doubling(val:Number):Number{ return val * 2; }
or
var doubling:Function = function(val:Number):Number{ return val * 2; }
In ActionScript 2.0, if you want to create a function that received no parameters, you must used Void type in the parameter list. In ActionScript 3.0 you must just leave the parameter list empty. And void (no capitalized) type is use as the function return type itself which defined the function to return nothing. And in ActionScript 2.0, if you not give anything at the parameter list, it means the function could receive arguments passed to it by the caller, and get the arguments using the arguments object in the function. But in ActionScript 3.0, by omitting parameter list, it really mean that the function will not received anything. And if you try to pass any arguments, it will return error. Instead, to able to receive arguments, you must use a new parameter type known as … (rest) in the parameter list and will written as …[argumentsArray] where the argumentsArray is name of the Array containing the arguments.
So function would look like :
function traceall(...vals):void{ trace(vals); } traceall("1", "a", true);//traces 1,a,true
Other Languages: