3.
Type the following code after the function:
var area:Number = getArea(10, 12);
trace(area); // 120
The
function call assigns the values 10 and 12 to the width and height,
getArea()
respectively, and you save the return value in the
that you save in the
4.
Select Control > Test Movie to test the SWF file.
You see
in the Output panel.
120
The parameters in the
exist while the function is called and cease to exist when the function exits.
In the next example, the ActionScript returns the value
enough parameters to the
To pass a variable number of parameters to a function:
1.
Create a new Flash document and save it as functionTest2.fla.
2.
Add the following code to Frame 1 of the main Timeline:
function addNumbers(a:Number, b:Number, c:Number):Number {
return (a + b + c);
}
trace(addNumbers(1, 4, 6)); // 11
trace(addNumbers(1, 4)); // NaN (Not a Number), c equals undefined
trace(addNumbers(1, 4, 6, 8)); // 11
If you don't pass enough parameters to the
are assigned a default value of
parameters are ignored.
3.
Select Control > Test Movie to test the Flash document.
Flash displays the following values: 11, NaN, 11.
Returning values from functions
You use the
statement to return values from functions. The
return
specifies the value that is returned by a function. The
an evaluation as a value of the function in which an expression executes. The
statement returns its result immediately to the calling code.
For more information, see
220
Functions and Methods
instance.
area
function are similar to values in a local variable; they
getArea()
function.
addNumbers()
undefined
return statement
instance. Then you trace the values
area
(not a number) if you don't pass
NaN
function, the missing arguments
addNumbers
. If you pass too many parameters, the excess
statement returns the result of
return
in the ActionScript 2.0 Language Reference.
statement
return
return
Need help?
Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?