0% found this document useful (0 votes)
5 views16 pages

Creative Web Des CH7

Chapter 7 introduces ActionScript 3.0, focusing on its core language and syntax, including variables, operators, expressions, comparisons, loops, and built-in functions. It explains variable scope, the importance of declaring variables with the 'var' statement, and the concept of operator precedence and associativity. Additionally, it covers regular expressions for pattern matching and string manipulation, as well as comparison operators for evaluating string values.

Uploaded by

Nursiti Dzulayha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views16 pages

Creative Web Des CH7

Chapter 7 introduces ActionScript 3.0, focusing on its core language and syntax, including variables, operators, expressions, comparisons, loops, and built-in functions. It explains variable scope, the importance of declaring variables with the 'var' statement, and the concept of operator precedence and associativity. Additionally, it covers regular expressions for pattern matching and string manipulation, as well as comparison operators for evaluating string values.

Uploaded by

Nursiti Dzulayha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

[Please insert any CHAPTER

7
relevant photo
around this box]

Automating Flash
With Action
Subtopics:-
7.1 Variables
7.2 Operators
7.3 Expressions
7.4 Comparisons
7.5 Loops
7.6 Built in functions

ActionScript 3.0 comprises both the core ActionScript language and the Adobe Flash
Player Application Programming Interface (API). The core language is the part of
ActionScript that defines the syntax of the language as well as the top-level data types.
ActionScript 3.0 provides programmatic access to Flash Player. This chapter provides a
brief introduction to the core ActionScript language and syntax. After reading this
chapter, you should have a basic understanding of how to work with data types and
variables, how to use proper syntax, and how to control the flow of data in your
program.

81 | P a g e C h a p t e r 7 : A u t o m a t i n g F l a s h W i t h A c t i o n
7.1 Variables

The scope of a variable is the area of your code where the variable can be accessed by
a lexical reference. A global variable is one that is defined in all areas of your code, where
as a local variable is one that is defined in only one part of your code. In ActionScript 3.0,
variables are always assigned the scope of the function or class in which they are
declared. A global variable is a variable that you define outside of any function or class
definition. For example, the following code creates a global variable strGlobal by
declaring it outside of any function. Variables allow you to store values that you use in
your program. To declare a variable, you must use the var statement with the variable
name. In ActionScript 2.0, use of the var statement is only required if you use type
annotations. In ActionScript 3.0, use of the var statement is always required. For example,
the following line of ActionScript declares a variable named i:
var i;

If you omit the var statement when declaring a variable, you will get a compiler error in
strict mode and run-time error in standard mode. For example, the following line of code
will result in an error if the variable i have not been previously defined:
i; // error if i was not previously defined

To associate a variable with a data type, you must do so when you declare the variable.
Declaring a variable without designating the variable’s type is legal, but will generate a
compiler warning in strict mode. You designate a variable’s type by appending the
variable name with a colon (:), followed by the variable’s type.

Understanding variable scope

The scope of a variable is the area of your code where the variable can be accessed by
a lexical reference. A global variable is one that is defined in all areas of your code,
whereas a local variable is one that is defined in only one part of your code. In
ActionScript 3.0, variables are always assigned the scope of the function or class in which
they are declared. A global variable is a variable that you define outside of any function
or class definition. For example, the following code creates a global variable strGlobal by
declaring it outside of any function.

You declare a local variable by declaring the variable inside a function definition. The
smallest area of code for which you can define a local variable is a function definition. A
local variable declared within a function will exist only in that function. If the variable

82 | P a g e C h a p t e r 7 : A u t o m a t i n g F l a s h W i t h A c t i o n
name you use for your local variable is already declared as a global variable, the local
definition hides (or shadows) the global definition while the local variable is in scope. The
global variable will still exist outside of the function. For example, the following code
creates a global string variable named str1, and then creates a local variable of the same
name inside the scopeTest() function. The trace statement inside the function outputs the
local value of the variable, but the trace statement outside the function outputs the
global value of the variable.

ActionScript variables, unlike variables in C++ and Java, do not have block-level scope.
A block of code is any group of statements between an opening curly brace ( { ) and a
closing curly brace ( } ). In some programming languages, such as C++ and Java,
variables declared inside a block of code are not available outside that block of code.
This restriction of scope is called block-level scope, and does not exist in ActionScript. If
you declare a variable inside a block of code, that variable will be available not only in
that block of code, but also in any other parts of the function to which the code block
belongs. For example, the following function contains variables that are defined in various
block scopes. All the variables are available throughout the function.

Default values

Data type Default value


Boolean False
int 0
Number NaN
Object null
String null
uint 0
Not declared (equivalent to type annotation * ) Undefined
All other classes, including user-defined classes. null

For variables of type Number, the default value is NaN (not a number), which is a special
value defined by the IEEE-754 standard to mean a value that does not represent a
number. If you declare a variable, but do not declare its data type, the default data type
* will apply, which actually means that the variable is untyped. If you also do not initialize
an untyped variable with a value, its default value is undefined.

For data types other than Boolean, Number, int, and uint, the default value of any
uninitialized variable is null. This applies to all the classes defined by ActionScript 3.0, as
well as any custom classes that you create. The value null is not a valid value for variables

83 | P a g e C h a p t e r 7 : A u t o m a t i n g F l a s h W i t h A c t i o n
of type Boolean, Number, int, or uint. If you attempt to assign a value of null to such a
variable, the value is converted to the default value for that data type. For variables of
type Object, you can assign a value of null. If you attempt to assign the value undefined
to a variable of type Object, the value is converted to null. For variables of type Number,
there is a special top-level function named isNaN() that returns the Boolean value true if
the variable is not a number, and false otherwise.

7.2 Operators

Operators are special functions that take one or more operands and return a value. An
operand is a value usually a literal, a variable, or an expression that an operator uses as
input. For example, in the following code, the addition (+) and multiplication (*) operators
are used with three literal operands (2, 3, and 4 ) to return a value. This value is then used
by the assignment (=) operator to assign the returned value, 14, to the variable
sumNumber .
var sumNumber:uint = 2 + 3 * 4; // uint = 14

Operators can be unary, binary, or ternary. A unary operator takes one operand. For
example, the increment (++) operator is a unary operator, because it takes only one
operand. A binary operator takes two operands. For example, the division (/) operator
takes two operands. A ternary operator takes three operands. For example, the
conditional (?:) operator takes three operands.

Some operators are overloaded, which means that they behave differently depending
on the type or quantity of operands passed to them. The addition (+) operator is an
example of an overloaded operator that behaves differently depending on the data
type of the operands. If both operands are numbers, the addition operator returns the
sum of the values. If both operands are strings, the addition operator returns the
concatenation of the two operands. The following example code shows how the
operator behaves differently depending on the operands:
trace(5 + 5); // 10
trace("5" + "5"); // 55

Operators can also behave differently based on the number of operands supplied. The
subtraction (-) operator is both a unary and binary operator. When supplied with only one
operand, the subtraction operator negates the operand and returns the result. When
supplied with two operands, the subtraction operator returns the difference between the

84 | P a g e C h a p t e r 7 : A u t o m a t i n g F l a s h W i t h A c t i o n
operands. The following example shows the subtraction operator used first as a unary
operator, and then as a binary operator.
trace(-3); // -3
trace(7 - 2); // 5

Operator precedence and associativity

Operator precedence and associativity determine the order in which operators are
processed. Although it may seem natural to those familiar with arithmetic that the
compiler processes the multiplication (*) operator before the addition ( ) operator, the
compiler needs explicit instructions about which operators to process first. Such
instructions are collectively referred to as operator precedence. ActionScript defines
default operator precedence that you can alter using the parentheses ( () ) operator. For
example, the following code alters the default precedence in the previous example to
force the compiler to process the addition operator before the multiplication operator:
var sumNumber:uint = (2 + 3) * 4; // uint == 20

You may encounter situations in which two or more operators of the same precedence
appear in the same expression. In these cases, the compiler uses the rules of associativity
to determine which operator to process first. All of the binary operators, except the
assignment operators, are left associative, which means that operators on the left are
processed before operators on the right. The assignment operators and the conditional (
?: ) operator are right-associative , which means that the operators on the right are
processed before operators on the left. For example, consider the less-than (<) and
greater-than (>) operators, which have the same precedence. If both operators are used
in the same expression, the operator on the left is processed first because both operators
are left-associative. This means that the following two statements produce the same
output:
trace(3 > 2 < 1); // false
trace((3 > 2) < 1); // false

The greater-than operator is processed first, which results in a value of true, because the
operand 3 is greater than the operand 2. The value true is then passed to the less-than
operator along with the operand 1. The following code represents this intermediate state:
trace((true) < 1);

The less-than operator converts the value true to the numeric value 1 and compares that
numeric value to the second operand 1 to return the value false (the value 1 is not less
than 1).

85 | P a g e C h a p t e r 7 : A u t o m a t i n g F l a s h W i t h A c t i o n
trace(1 < 1); // false

You can alter the default left associativity with the parentheses operator. You can instruct
the compiler to process the less-than operator first by enclosing that operator and its
operands in parentheses. The following example uses the parentheses operator to
produce a different output using the same numbers as the previous example:
trace(3 > (2 < 1)); // true

The less-than operator is processed first, which results in a value of false because the
operand 2 is not less than the operand 1. The value false is then passed to the greater-
than operator along with the operand 3. The following code represents this intermediate
state:
trace(3 > (false));

The greater-than operator converts the value false to the numeric value 0 and compares
that numeric value to the other operand 3 to return true (the value 3 is greater than 0).
trace(3 > 0); // true

The following table lists the operators for ActionScript 3.0 in order of decreasing
precedence. Each row of the table contains operators of the same precedence. Each
row of operators has higher precedence than the row appearing below it in the table.
Group Operators
Primary [] {x:y} () f(x) new x.y x[y] <></> @ :: ..
Postfix x++ x--
Unary ++x --x + - ~ ! delete typeof void
Multiplicative */%
Additive +-
Bitwise shift << >> >>>
Relational < > <= >= as in instance of is
Equality == != === !==
Bitwise AND &
Bitwise XOR ^
Bitwise OR |
Logical AND &&
Logical OR ||
Conditional ?:
Assignment = *= /= %= += -= <<= >>= >>>= &= ^= |=
Comma ,

7.3 Expressions

A regular expression describes a pattern of characters. Regular expressions are typically


used to verify that a text value conforms to a particular pattern (such as verifying that a
user-entered phone number has the proper number of digits) or to replace portions of a
text value that matches a particular pattern. Regular expressions can be simple. For
example, suppose you wanted to confirm that a particular string matches “ABC,” or
wanted to replace every occurrence of “ABC” in a string with some other text. In that

86 | P a g e C h a p t e r 7 : A u t o m a t i n g F l a s h W i t h A c t i o n
case, you could use the following regular expression, which defines the pattern consisting
of the letters A, B, and C in sequence:
/ABC/

Regular expression patterns can also be complex, and sometimes cryptic in appearance,
such as the following expression to match a valid e-mail address:
/([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}/

Most commonly you will use regular expressions to search for patterns in strings and to
replace characters. In those cases, you will create a regular expression object and use it
as a parameter for one of several String class methods. The following methods of the String
class take regular expressions as parameters: match(), replace(), search(), and split(). For
more information on these methods, see Finding patterns in strings and replacing
substrings. The RegExp class includes the following methods: test() and exec().

Common regular expression tasks

• Creating a regular expression pattern


• Using special characters in patterns
• Identifying sequences of multiple characters (such as “a two-digit number” or “between
seven and ten letters”)
• Identifying any character in a range of letters or numbers (such as “any letter from a to
m”)
• Identifying a character in a set of possible characters
• Identifying subsequences (segments within a pattern)
• Matching and replacing text based on patterns

Important concepts and terms

• Escape character: A character indicating that the character that follows should be
treated as a metacharacter rather than a literal character. In regular expression syntax,
the backslash character (\) is the escape character, so a backslash followed by another
character is a special code rather than just the character itself.
• Flag: A character that specifies some option about how the regular expression pattern
should be used, such as whether to distinguish between uppercase and lowercase
characters.
• Metacharacter: A character that has special meaning in a regular expression pattern, as
opposed to literally representing that character in the pattern.

87 | P a g e C h a p t e r 7 : A u t o m a t i n g F l a s h W i t h A c t i o n
• Quantifier: A character (or several characters) indicating how many times a part of the
pattern should repeat. For example, a quantifier would be used to designate that a United
States postal code should contain five or nine numbers.
• Regular expression: A program statement defining a pattern of characters that can be
used to confirm whether other strings match that pattern or to replace portions of a string.

7.4 Comparisons

You can use the following operators to compare strings: <, <=, !=, ==, =>, and >. These
operators can be used with conditional statements, such as if and while, as the following
example shows:
var str1:String = "Apple";
var str2:String = "apple";
if (str1 < str2)
{
trace("A < a, B < b, C < c, ...");
}
When using these operators with strings, ActionScript considers the character code value
of each character in the string, comparing characters from left to right, as in the following:
trace("A" < "B"); // true
trace("A" < "a"); // true
trace("Ab" < "az"); // true
trace("abc" < "abza"); // true
Use the == and != operators to compare strings with each other and to compare strings
with other types of objects, as the following example shows:
var str1:String = "1";
var str1b:String = "1";
var str2:String = "2";
trace(str1 == str1b); // true
trace(str1 == str2); // false
var total:uint = 1;
trace(str1 == total); // true

7.5 Loops

Looping statements allow you to perform a specific block of code repeatedly using a
series of values or variables. Adobe recommends that you always enclose the block of
code in braces ( {} ). Although you can omit the braces if the block of code contains only
one statement, this practice is not recommended for the same reason that it is not
recommended for conditionals: it increases the likelihood that statements added later
will be inadvertently excluded from the block of code. If you later add a statement that
you want to include in the block of code, but forget to add the necessary braces, the
statement will not be executed as part of the loop.

88 | P a g e C h a p t e r 7 : A u t o m a t i n g F l a s h W i t h A c t i o n
For

The for loop allows you to iterate through a variable for a specific range of values. You
must supply three expressions in a for statement: a variable that is set to an initial value, a
conditional statement that determines when the looping ends, and an expression that
changes the value of the variable with each loop. For example, the following code loops
five times. The value of the variable i starts at 0 and ends at 4, and the output will be the
numbers 0 through 4, each on its own line.
var i:int;
for (i = 0; i < 5; i++)
{
trace(i);
}

for..in

The for..in loop iterates through the properties of an object, or the elements of an array.
For example, you can use a for..in loop to iterate through the properties of a generic
object (object properties are not kept in any particular order, so properties may appear
in a seemingly random order):
var myObj:Object = {x:20, y:30};
for (var i:String in myObj)
{
trace(i + ": " + myObj[i]);
}
// output:
// x: 20
// y: 30
You can also iterate through the elements of an array:
var myArray:Array = ["one", "two", "three"];
for (var i:String in myArray)
{
trace(myArray[i]);
}
// output:
// one
// two
// three
What you cannot do is iterate through the properties of an object if it is an instance of a
user-defined class, unless the class is a dynamic class. Even with instances of dynamic
classes, you will be able to iterate only through properties that are added dynamically.

for each..in

The for each..in loop iterates through the items of a collection, which can be tags in an
XML or XML List object, the values held by object properties, or the elements of an array.
For example, as the following excerpt shows, you can use a for each..in loop to iterate

89 | P a g e C h a p t e r 7 : A u t o m a t i n g F l a s h W i t h A c t i o n
through the properties of a generic object, but unlike the for..in loop, the iterator variable
in a for each..in loop contains the value held by the property instead of the name of the
property:
var myObj:Object = {x:20, y:30};
for each (var num in myObj)
{
trace(num);
}
// output:
// 20
// 30
You can iterate through an XML or XMLList object, as the following example shows:
var myXML:XML = <users>
<fname>Jane</fname>
<fname>Susan</fname>
<fname>John</fname>
</users>;

for each (var item in myXML.fname)


{
trace(item);
}
/* output
Jane
Susan
John
*/

You can also iterate through the elements of an array, as this example shows:
var myArray:Array = ["one", "two", "three"];
for each (var item in myArray)
{
trace(item);
}
// output:
// one
// two
// three
You cannot iterate through the properties of an object if the object is an instance of a
sealed class. Even for instances of dynamic classes, you cannot iterate through any fixed
properties, which are properties defined as part of the class definition.

While

The while loop is like an if statement that repeats as long as the condition is true . For
example, the following code produces the same output as the for loop example:
var i:int = 0;
while (i < 5)
{
trace(i);
i++;
}

90 | P a g e C h a p t e r 7 : A u t o m a t i n g F l a s h W i t h A c t i o n
One disadvantage of using a while loop instead of a for loop is that infinite loops are
easier to write with while loops. The for loop example code does not compile if you omit
the expression that increments the counter variable, but the while loop example does
compile if you omit that step. Without the expression that increments i , the loop becomes
an infinite loop.

do..while

The do..while loop is a while loop that guarantees that the code block is executed at
least once, because the condition is checked after the code block is executed. The
following code shows a simple example of a do..while loop that generates output even
though the condition is not met:
var i:int = 5;
do
{
trace(i);
i++;
} while (i < 5);
// output: 5

7.6 Built in functions

You call a function by using its identifier followed by the parentheses operator ( () ). You
use the parentheses operator to enclose any function parameters you want to send to
the function. For example, the trace() function, which is a top-level function in
ActionScript 3.0, is used throughout this book:
trace("Use trace to help debug your script");
If you are calling a function with no parameters, you must use an empty pair of
parentheses. For example, you can use the Math.random() method, which takes no
parameters, to generate a random number:
var randomNum:Number = Math.random();

Defining your own functions

There are two ways to define a function in ActionScript 3.0: you can use a function
statement or a function expression. The technique you choose depends on whether you
prefer a more static or dynamic programming style. Define your functions with function
statements if you prefer static, or strict mode, programming. Define your functions with
function expressions if you have a specific need to do so. Function expressions are more
often used in dynamic, or standard mode, programming.

91 | P a g e C h a p t e r 7 : A u t o m a t i n g F l a s h W i t h A c t i o n
Function statements

Function statements are the preferred technique for defining functions in strict mode. A
function statement begins with the function keyword, followed by:
• The function name
• The parameters, in a comma-delimited list enclosed in parentheses
• The function body—that is, the ActionScript code to be executed when the
function is invoked, enclosed in curly braces. For example, the following code
creates a function that defines a parameter and then invokes the function using
the string “ hello" as the parameter value:
function traceParameter(aParam:String)
{
trace(aParam);
}

traceParameter("hello"); // hello

Function expressions

The second way to declare a function is to use an assignment statement with a function
expression, which is also sometimes called a function literal or an anonymous function.
This is a more verbose method that is widely used in earlier versions of ActionScript. An
assignment statement with a function expression begins with the var keyword, followed
by:
• The function name
• The colon operator ( : )
• The Function class to indicate the data type
• The assignment operator ( = )
• The function keyword
• The parameters, in a comma-delimited list enclosed in parentheses
• The function body—that is, the ActionScript code to be executed when
the function is invoked, enclosed in curly braces. For example, the
following code declares the traceParameter function using a function
expression:
var traceParameter:Function = function (aParam:String)
{
trace(aParam);
};
traceParameter("hello"); // hello
Notice that you do not specify a function name, as you do in a function statement.
Another important difference between function expressions and function statements is

92 | P a g e C h a p t e r 7 : A u t o m a t i n g F l a s h W i t h A c t i o n
that a function expression is an expression rather than a statement. This means that a
function expression cannot stand on its own as a function statement can. A function
expression can be used only as a part of a statement, usually an assignment statement.
The following example shows a function expression assigned to an array element:
var traceArray:Array = new Array();
traceArray[0] = function (aParam:String)
{
trace(aParam);
};
traceArray[0]("hello");

Choosing between statements and expressions

As a general rule, use a function statement unless specific circumstances call for the use
of an expression. Function statements are less verbose, and they provide a more
consistent experience between strict mode and standard mode than function
expressions.

Function statements are easier to read than assignment statements that contain function
expressions. Function statements make your code more concise; they are less confusing
than function expressions, which require you to use both the var and function keywords.

Function statements provide a more consistent experience between the two compiler
modes in that you can use dot syntax in both strict and standard mode to invoke a
method declared using a function statement. This is not necessarily true for methods
declared with a function expression. For example, the following code defines a class
named Example with two methods: methodExpression() , which is declared with a
function expression, and methodStatement() , which is declared with a function
statement. In strict mode, you cannot use dot syntax to invoke the methodExpression()
method.
class Example
{
var methodExpression = function() {}
function methodStatement() {}
}

var myEx:Example = new Example();


myEx.methodExpression(); // error in strict mode; okay in standard mode
myEx.methodStatement(); // okay in strict and standard modes

Function expressions are considered better suited to programming that focuses on run-
time, or dynamic, behavior. If you prefer to use strict mode, but also need to call a
method declared with a function expression, you can use either of two techniques. First,

93 | P a g e C h a p t e r 7 : A u t o m a t i n g F l a s h W i t h A c t i o n
you can call the method using square brackets ( [] ) instead of the dot ( . ) operator. The
following method call succeeds in both strict mode and standard mode:
myExample["methodLiteral"]();
Second, you can declare the entire class as a dynamic class. Although this allows you to
call the method using the dot operator, the downside is that you sacrifice some strict
mode functionality for all instances of that class. For example, the compiler does not
generate an error if you attempt to access an undefined property on an instance of a
dynamic class. There are some circumstances in which function expressions are useful.
One common use of function expressions is for functions that are used only once and
then discarded. Another less common use is for attaching a function to a prototype
property. For more information, see the prototype object.

There are two subtle differences between function statements and function expressions
that you should take into account when choosing which technique to use. The first
difference is that function expressions do not exist independently as objects with regard
to memory management and garbage collection. In other words, when you assign a
function expression to another object, such as an array element or an object property,
you create the only reference to that function expression in your code. If the array or
object to which your function expression is attached goes out of scope or is otherwise no
longer available, you will no longer have access to the function expression. If the array or
object is deleted, the memory that the function expression uses will become eligible for
garbage collection, which means that the memory is eligible to be reclaimed and reused
for other purposes. The following example shows that for a function expression, once the
property to which the expression is assigned is deleted, the function is no longer available.
The class Test is dynamic, which means that you can add a property named functionExp
that holds a function expression. The functionExp() function can be called with the dot
operator, but once the functionExp property is deleted, the function is no longer
accessible.
dynamic class Test {}
var myTest:Test = new Test();

// function expression
myTest.functionExp = function () { trace("Function expression") };
myTest.functionExp(); // Function expression
delete myTest.functionExp;
myTest.functionExp(); // error
If, on the other hand, the function is first defined with a function statement, it exists as its
own object and continues to exist even after you delete the property to which it is

94 | P a g e C h a p t e r 7 : A u t o m a t i n g F l a s h W i t h A c t i o n
attached. The delete operator only works on properties of objects, so even a call to
delete the function stateFunc() itself does not work.
dynamic class Test {}
var myTest:Test = new Test();

// function statement
function stateFunc() { trace("Function statement") }
myTest.statement = stateFunc;
myTest.statement(); // Function statement
delete myTest.statement;
delete stateFunc; // no effect
stateFunc();// Function statement
myTest.statement(); // error
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 scopeTest() 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 return statement followed by the expression
or 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 terminates the function, so that any statements below a
return statement will not be executed, as follows:
function doubleNum(baseNum:int):int {
return (baseNum * 2);
trace("after return"); // This trace statement will not be executed.
}

95 | P a g e C h a p t e r 7 : A u t o m a t i n g F l a s h W i t h A c t i o n
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");
}

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 "10";
}
function getProductName():String
{
return "Flash Player";
}
return (getProductName() + " " + getVersion());
}
trace(getNameAndVersion()); // Flash Player 10

96 | P a g e C h a p t e r 7 : A u t o m a t i n g F l a s h W i t h A c t i o n

You might also like