function statement

Usage 1: (Declares a named function.)
function functionname([parameter0, parameter1,...parameterN]){
statement(s)
}
Usage 2: (Declares an anonymous function and returns a reference to it.)
function ([parameter0, parameter1,...parameterN]){ 
statement(s)
} 

Comprises a set of statements that you define to perform a certain task. You can define a function in one location and invoke, or call, 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 so you can reuse a function in different situations.

Use the return statement in a function's statement(s) to cause a function to generate, or return, a value.

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); 
}

The following function creates a LoadVars object and loads params.txt into the SWF file. When the file successfully loads, variables loaded traces:

var myLV:LoadVars = new LoadVars(); 
myLV.load("params.txt"); 
myLV.onLoad = function(success:Boolean) { 
 trace("variables loaded"); 
}

Flash CS3