User Guide
Statements 213
You can use this statement to define a function with the specified functionname,
parameters, and statement(s). When a script calls a function, the statements in the
function's definition are executed. Forward referencing is permitted; within the same script, a
function may be declared after it is called. A function definition replaces any previous
definition of the same function. You can use this syntax wherever a statement is permitted.
You can use the function statement to create an anonymous function and return a reference to
it. This syntax is used in expressions and is particularly useful for installing methods in
objects.
For additional functionality, you can use the
arguments object in your function definition.
Some common uses of the
arguments object are to create a function that accepts a variable
number of parameters and to create a recursive anonymous function.
Availability: ActionScript 1.0; Flash Player 5
Returns
String - Usage 1: The declaration form does not return anything. Usage 2: A reference to the
anonymous function.
Parameters
functionname:String - The name of the declared function.
Example
The following example defines the function
sqr, which accepts one parameter and returns the
Math.pow(x, 2) of the parameter:
function sqr(x:Number) {
return Math.pow(x, 2);
}
var y:Number = sqr(3);
trace(y); // output: 9
If the function is defined and used in the same script, the function definition may appear after
using the function:
var y:Number = sqr(3);
trace(y); // output: 9
function sqr(x:Number) {
return Math.pow(x, 2);
}