De-I > function
functionSyntax
functionfunctionname
([argument0, argument1,...argumentN
]
){
statement(s)
}
function ([argument0, argument1,...argumentN
]
){
statement(s)
}
Arguments
functionname
The name of the new function.
argument
Zero or more strings, numbers, or objects to pass the function
.
statements
Zero or more ActionScript statements you have defined for the body of the function
.
Description
Action; 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 movie. When you define a function, you can also specify arguments for the function. Arguments are placeholders for values on which the function will operate. You can pass a function different arguments, also called parameters, each time you call it.
Use the return
action in a functions statement(s)
to cause a function to return, or generate, a value.
Usage 1: Declares a function
with the specified functionname
, arguments
, 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.
Player
Flash 5 or later.
Example
(Usage 1) The following example defines the function sqr
, which accepts one argument, and returns the square(x*x)
of the argument. Note that 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; }
(Usage 2) The following function defines a Circle object:
function Circle(radius) { this.radius = radius; }
The following statement defines an anonymous function that calculates the area of a circle and attaches it to the object Circle
as a method:
Circle.prototype.area = function () {return Math.PI * this.radius * this.radius}