Creating functions
43
The parameter
initials
in the function
fillOutScorecard()
is similar to a local variable; it
exists while the function is called and ceases to exist when the function exits. If you omit
parameters during a function call, the omitted parameters are passed as
undefined
. If you provide
extra parameters in a function call that are not required by the function declaration, they
are ignored.
Using variables in a function
Local variables are valuable tools for organizing code and making it easy to understand. When a
function uses local variables, it can hide its variables from all other scripts in the SWF file; local
variables are scoped to the body of the function and cease to exist when the function exits. Any
parameters passed to a function are also treated as local variables.
You can also use global and regular variables in a function. However, if you modify global or
regular variables, it is good practice to use script comments to document these modifications.
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
statement. The following rules govern how
to use the
return
statement in functions:
•
If you specify a return type other than Void for a function, you must include a
return
statement followed by the returned value in the function.
•
If you specify a return type of Void, you generally do not need to include a
return
statement.
•
No matter what the return type, you can use a return statement to exit from the middle of a
function, provided the return statement is followed by a return value, according to the previous
rules.
•
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):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;
}
Summary of Contents for FLEX-FLEX ACTIONSCRIPT LANGUAGE
Page 1: ...Flex ActionScript Language Reference...
Page 8: ......
Page 66: ...66 Chapter 2 Creating Custom Classes with ActionScript 2 0...
Page 76: ......
Page 133: ...break 133 See also for for in do while while switch case continue throw try catch finally...
Page 135: ...case 135 See also break default strict equality switch...
Page 146: ...146 Chapter 5 ActionScript Core Language Elements See also break continue while...
Page 808: ...808 Chapter 7 ActionScript for Flash...
Page 810: ...810 Appendix A Deprecated Flash 4 operators...
Page 815: ...Other keys 815 Num Lock 144 186 187 _ 189 191 192 219 220 221 222 Key Key code...
Page 816: ...816 Appendix B Keyboard Keys and Key Code Values...
Page 822: ...822 Index...