User Guide

92 ActionScript Language and Syntax
Defining your own functions
There are two ways to define a function in ActionScript 3.0: you can use a function statement
or a function expression. The technique you choose depends on whether you prefer a more
static or dynamic programming style. Define your functions with function statements if you
prefer static, or strict mode, programming. Define your functions with function expressions if
you have a specific need to do so. Function expressions are more often used in dynamic, or
standard mode, programming.
Function statements
Function statements are the preferred technique for defining functions in strict mode. A
function statement begins with the
function keyword, followed by:
The function name.
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 creates a function that defines a parameter and then invokes
the function using the string “
hello” as the parameter value:
function traceParameter(aParam:String)
{
trace(aParam);
}
traceParameter("hello"); // hello
Function expressions
The second way to declare a function is to use an assignment statement with a function
expression, which is also sometimes called a function literal or an anonymous function. This
is a more verbose method that is widely used in earlier versions of ActionScript.