User Guide

Creating functions 63
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):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:Number = this.mathLib.sqr(3);
The following example uses an path to call the initialize() function that was defined on the
main Timeline and requires no parameters:
this.initialize();
The following example uses a relative path to call the list() function that was defined in the
functionsClip movie clip:
this._parent.functionsClip.list(6);