EasyManua.ls Logo

MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT - Page 53

MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT
816 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
Creating functions 53
Returning values from a function
Use the
return statement to return values from functions. The return statement stops the
function and replaces it with the value of the
return action. The following rules govern the use of
the
return statement in functions:
If you specify a return type other than void for a function, you must include a return
statement in the function.
If you specify a return type of void, you should not include a return statement.
If you dont specify a return type, including a return statement is optional. If you dont
include one, an empty string is returned.
For example, the following function returns the square of the parameter
x and specifies that the
returned value must be a Number:
function sqr(x):Number {
return x * x;
}
Some functions perform a series of tasks without returning a value. For example, the following
function initializes a series of global variables:
function initialize() {
boat_x = _global.boat._x;
boat_y = _global.boat._y;
car_x = _global.car._x;
car_y = _global.car._y;
}
Calling a user-defined function
You can use a target path to call a function in any Timeline from any Timeline, including from
the Timeline of a loaded SWF file. If a function was declared using the
_global identifier, you do
not need to use a target path to call it.
To call a function, enter the target path to the name of the function, if necessary, and pass any
required parameters inside parentheses. For example, the following statement invokes the
function
sqr() in the movie clip MathLib on the main Timeline, passes the parameter 3 to it,
and stores the result in the variable
temp:
var temp = _root.MathLib.sqr(3);
The following example uses an absolute path to call the initialize() function that was defined
on the main Timeline and requires no parameters:
_root.initialize();
The following example uses a relative path to call the list() function that was defined in the
functionsClip movie clip:
_parent.functionsClip.list(6);

Table of Contents

Related product manuals