9.1. Creating Functions
To
make
a basic function we simply need a function name
and a block of statements to perform, like this:
function funcName ( ) {
statements
}
The function keyword starts the
declaration
of
our new function. Next comes our function name,
funcName, which we'll use later to
invoke our
function. funcName must be a legal
identifier.[1] Next, we
supply a pair of parentheses, ( ), that enclose
any optional parameters, which we'll discuss later. If our
function does not have any parameters, we leave the parentheses
empty. Finally, we provide the function
body
(i.e., statement block), which contains the code that's
executed when our function is
called.
Let's make a (very) simple function:
Start a new Flash
movie.
On frame 1 of the main movie timeline, attach the following code:
function sayHi ( ) {
trace("Hi there!");
}
That was easy -- we've just created a function named
sayHi( ). When we run it, the trace(
) statement in its body will be executed. Don't
close the movie you've created, we'll learn to run our
function next.