User Guide

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 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):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;
}