User Guide

Functions 93
An assignment statement with a function expression begins with the var keyword, followed by:
The function name
The colon operator (:)
The Function class to indicate the data type
The assignment operator (=)
The function keyword
The parameters, in a comma-delimited list enclosed in parentheses
The function body—that is, the ActionScript code to be executed when the function is
invoked, enclosed in curly braces
For example, the following code declares the
traceParameter function using a function
expression:
var traceParameter:Function = function (aParam:String)
{
trace(aParam);
};
traceParameter("hello"); // hello
Notice that you do not specify a function name, as you do in a function statement. Another
important difference between function expressions and function statements is that a function
expression is an expression rather than a statement. This means that a function expression
cannot stand on its own, as a function statement can. A function expression can be used only
as a part of a statement, usually an assignment statement. The following example shows a
function expression assigned to an array element:
var traceArray:Array = new Array();
traceArray[0] = function (aParam:String)
{
trace(aParam);
};
traceArray[0]("hello");
Choosing between statements and expressions
As a general rule, use a function statement unless specific circumstances call for the use of an
expression. Function statements are less verbose, and they provide a more consistent
experience between strict mode and standard mode than function expressions.
Function statements are easier to read than assignment statements that contain function
expressions. Function statements make your code more concise; they are less confusing than
function expressions, which require you to use both the
var and function keywords.