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 everything 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 program functions— the functions we create ourselves in our scripts. By learning to create our own functions, we’ll become familiar with these fundamentals:

Function declaration

Creating functions to 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.

Once we understand those aspects of functions, we’ll consider how they apply to internal functions, functions that come built into ActionScript. Let’s get to it!

Creating Functions

To make a basic function ...

Get ActionScript: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.