User Guide
Functions 97
Nested functions
You can nest functions, which means that functions can be declared within other functions. A
nested function is available only within its parent function unless a reference to the function is
passed to external code. For example, the following code declares two nested functions inside
the
getNameAndVersion() function:
function getNameAndVersion():String
{
function getVersion():String
{
return "9";
}
function getProductName():String
{
return "Flash Player";
}
return (getProductName() + " " + getVersion());
}
trace(getNameAndVersion()); // Flash Player 9
When nested functions are passed to external code, they are passed as function closures, which
means that the function retains any definitions that are in scope when the function is defined.
For more information, see “Function closures” on page 104.
Function parameters
ActionScript 3.0 provides some functionality for function parameters that may seem novel for
programmers new to the language. Although the idea of passing parameters by value or
reference should be familiar to most programmers, the
arguments object and the ... (rest)
parameter may be new to many of you.
Passing arguments by value or by reference
In many programming languages, it’s important to understand the distinction between
passing arguments by value or by reference; the distinction can affect the way code is
designed.
To be passed by value means that the value of the argument is copied into a local variable for
use within the function. To be passed by reference means that only a reference to the
argument is passed, instead of the actual value. No copy of the actual argument is made.
Instead, a reference to the variable passed as an argument is created and assigned to a local
variable for use within the function. As a reference to a variable outside the function, the local
variable gives you the ability to change the value of the original variable.