
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
190
|
Chapter 9: Functions
Regardless of whether the return statement is implied or explicit, whenever a func-
tion terminates, execution resumes at the line of code following the function invoca-
tion. For example:
say("Something"); // This executes the code in the say() function
// Execution resumes here after the say() function terminates
trace("Something else");
Returning Values from Functions
As we’ve seen, return always terminates a function. But it can also be used to send a
value back to the script that invoked the function, using the following syntax:
return expression;
The value of expression becomes the result of the function invocation. For example:
// Define a function that adds two numbers
function combine (a, b) {
return a + b; // Return the sum of the two arguments
}
// Invoke the function
var total = combine(2, 1); // Sets total to 3
The expression or result returned by the return statement is called the return value of
the function.
Notice that our combine() function merely calculates and returns the sum of two
numbers (it will also concatenate two strings). It does not perform an overt action, as
did the sayHi() function (which displayed a message) or the moveClip() function
(which repositioned a movie clip).
We can make use of a function’s return value by assigning it to a ...