
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
184
|
Chapter 9: Functions
The parentheses contain the values for any parameters required by the function. If
the function requires no parameters (as determined by the function declaration), the
parentheses are left empty. Let’s try invoking our sayHi() function, which has no
parameters, to get the hang of basic function invocation.
Here, again, is our sayHi() function declaration:
function sayHi () {
trace("Hi there!");
}
And here’s our sayHi( ) function invocation:
sayHi();
In this simple demonstration, we add the function invocation to the same script con-
taining the function declaration, but we’ll see later that this isn’t always the case.
When the sayHi( ) function call is executed, the sayHi function is invoked, so its
function body runs, causing “Hi there!” to appear in the Output window.
You can see that typing sayHi(); is more convenient than typing the whole trace()
statement, and the function name, sayHi, is a more meaningful description of what
our code does. Using thoughtful function names makes our code more readable,
almost like human sentences. In this book and in most programming documenta-
tion, function names are conventionally listed with the invocation parentheses, as in
“the sayHi( ) function” versus “the sayHi function.” This convention makes function
names easy to ...