Adobe FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0 Manual page 96

Programming actionscript 3.0
Table of Contents

Advertisement

The second difference between function statements and function expressions is that function
statements exist throughout the scope in which they are defined, including in statements that
appear before the function statement. Function expressions, by contrast, are defined only for
subsequent statements. For example, the following code successfully calls the
function before it is defined:
statementTest(); // statementTest
function statementTest():void
{
trace("statementTest");
}
Function expressions are not available before they are defined, so the following code results in
a run-time error:
expressionTest(); // run-time error
var expressionTest:Function = function ()
{
trace("expressionTest");
}
Returning values from functions
To return a value from your function, use the
literal value that you want to return. For example, the following code returns an expression
representing the parameter:
function doubleNum(baseNum:int):int
{
return (baseNum * 2);
}
Notice that the
return
statement will not be executed, as follows:
return
function doubleNum(baseNum:int):int {
return (baseNum * 2);
trace("after return"); // This trace statement will not be executed.
}
In strict mode, you must return a value of the appropriate type if you choose to specify a
return type. For example, the following code generates an error in strict mode because it does
not return a valid value:
function doubleNum(baseNum:int):int
{
trace("after return");
}
96
ActionScript Language and Syntax
statement terminates the function, so that any statements below a
statement followed by the expression or
return
scopeTest()

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents