flash language_api_functions
Functions
ActionScript 3 integrates new functionalities linked to the definition of functions. Take the following case where a function is showing a personalized message:
function alert ( pMessage:String ):void { trace( pMessage ); }
This alert function accepts a parameter: the message to be displayed. If we want to execute it all we have to do send a message:
alert ("here is the alert message!");
If we omit the parameter:
// generates a compilation error alert ();
The following compilation error is generated:
1136: Number of arguments incorrect. 1 expected.
ActionScript 3 allows you to define a default value for the parameter:
function alert ( pMessage:String="default message" ):void { trace( pMessage ); }
Once defined we can call the alert function without using parameters:
function alert ( pMessage:String="default message" ):void { trace( pMessage ); } // display : default message alert ();
When we use a specific parameter it replaces the default value:
function alert ( pMessage:String="default message" ):void { trace( pMessage ); } // display : a personalised message! alert ( "a personalised message!" );
As well as that, ActionScript 3 has integrated a new mechanism linked to random parameters. Imagine that we need to create a function which can take a random number of parameters. In ActionScript 1 and 2 we couldn’t indicate the function signature. We therefore defined a function without parameters then used the arguments table which regrouped all of the parameters:
function averageCalculation ():Number { var lng:Number = arguments.length; var total:Number = 0; for (var i:Number = 0; i< lng; i++) { total += arguments[i]; } return total / lng; } var average :Number = averageCalculation ( 50, 48, 78, 20, 90 ); // display : 57.2 trace( average );
Even though this method seems quite supple it does however pose a problem when rereading the code. When reading the signature code a developer could think that the averageCalculation function wouldn’t take any parameter, which is not the case.
In order to resolve this problem, ActionScript 3 has introduced a keyword which allows you to specify in the parameters that the function receives a variable number of parameters. To do this we add three full stops (an ellipsis) as well as the function parameter, followed by the variable name of our choice. The same code in ActionScript 3 is therefore written in the following way:
function averageCalculation ( ...parameters ):Number { var lng:int = parameters.length; var total:Number = 0; for (var i:Number = 0; i< lng; i++) { total += parameters[i]; } return total / lng; } var average:Number = averageCalculation ( 50, 48, 78, 20, 90 ); // display : 57.2 trace( average );
When rereading the previous code, an ActionScript 3 developer can easily detect the functions which accept a random number of parameters.
Mediabox Training Centre © 2000 - 2008 All rights reserved.
Adobe Authorized Training Centre. State convention under number 25 14 02167 14.
Mediabox : SARL au capital de 62.000€ - Activity number: 25 14 02167 14 - SIRET : 493 716 468 00027
MEDIABOX, 102 Avenue des Champs Elysées, 75008 PARIS - Tel. +33(0)2.31.91.96.89 - Fax. +33(0)2.72.68.56.42


