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 don’t specify a return type, including a
return
statement is optional. If you don’t
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);
Summary of Contents for FLASH MX 2004 - ACTIONSCRIPT
Page 1: ...ActionScript Reference Guide...
Page 8: ...8 Contents...
Page 12: ......
Page 24: ...24 Chapter 1 What s New in Flash MX 2004 ActionScript...
Page 54: ...54 Chapter 2 ActionScript Basics...
Page 80: ...80 Chapter 3 Writing and Debugging Scripts...
Page 82: ......
Page 110: ...110 Chapter 5 Creating Interaction with ActionScript...
Page 112: ......
Page 120: ...120 Chapter 6 Using the Built In Classes...
Page 176: ......
Page 192: ...192 Chapter 10 Working with External Data...
Page 202: ...202 Chapter 11 Working with External Media...
Page 204: ......
Page 782: ...782 Chapter 12 ActionScript Dictionary...
Page 793: ...Other keys 793 221 222 Key Key code...
Page 794: ...794 Appendix C Keyboard Keys and Key Code Values...
Page 798: ...798 Appendix D Writing Scripts for Earlier Versions of Flash Player...
Page 806: ...806 Appendix E Object Oriented Programming with ActionScript 1...
Page 816: ...816 Index...