Table of Contents

Chapter 9. Functions

I'm almost giddy to tell you about functions because they're such a powerful part of ActionScript. A function is simply a chunk of code that can be reused throughout a program. Not only do functions lend enormous flexibility and convenience to our scripts, they also give us control over Flash movie elements. I can hardly imagine programming without functions�they ease performing common operations, from sorting words to calculating the distance between two movie clips. We'll introduce functions in this chapter before learning how to create complex, powerful programs using functions with objects in Chapter 12.

We'll focus first on the simplest functions to understand�custom functions�the functions we create in our scripts, as distinguished from the built-in functions provided with ActionScript.

The ECMA-262 specification refers to custom functions as program functions and built-in functions as internal functions. We use the colloquial name in both cases rather than the formal specification's terminology.

By learning to create your own functions, you'll become familiar with these fundamentals:

Function declaration or function definition

Creating functions for future use in our scripts.

Function invocation

Causing functions to execute. In other words, running the code in a function. Also known as calling a function.

Function arguments and parameters

Providing functions with data to manipulate upon invocation.

Function termination

Ending the execution of a function and, optionally, returning a result.

Function scope

Determining the availability and life span of a function, and the accessibility of variables referenced in a function body.

Let's get to it!


Table of Contents