388 Chapter 12: ActionScript Dictionary
function
Availability
Flash Player 5.
Usage
function functionname ([parameter0, parameter1,...parameterN]){
statement(s)
}
function ([parameter0, parameter1,...parameterN]){
statement(s)
}
Parameters
functionname
The name of the new function.
parameter An identifier that represents a parameter to pass to the function. These parameters
are optional.
statement(s) Any ActionScript instruction you have defined for the body of the function.
Returns
Nothing.
Description
Statement; comprises a set of statements that you define to perform a certain task. You can
declare, or define, a function in one location and call, or invoke, it from different scripts in a SWF
file. When you define a function, you can also specify parameters for the function. Parameters are
placeholders for values on which the function operates. You can pass different parameters to a
function each time you call it. This lets you reuse one function in many different situations.
Use the
return action in a function’s statement(s) to cause a function to return, or generate,
a value.
Usage 1: Declares a
function with the specified functionname, parameters, and
statement(s). When a function is called, the function declaration is invoked. Forward
referencing is permitted; within the same Action list, a function may be declared after it is called.
A function declaration replaces any prior declaration of the same function. You can use this syntax
wherever a statement is permitted.
Usage 2: Creates an anonymous function and returns it. This syntax is used in expressions, and is
particularly useful for installing methods in objects.
Example
Usage 1: The following example defines the function sqr, which accepts one parameter and
returns the
square(x*x) of the parameter. If the function is declared and used in the same script,
the function declaration may appear after using the function.
y=sqr(3);
function sqr(x) {
return x*x;
}