O'Reilly logo

ActionScript 3.0 Bible by Roger Braunstein

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 3. Functions and Methods

Now that you know all about creating variables, you probably want to start actually doing something with them. This is where functions and methods come in. Functions are reusable blocks of code, also called methods when they are part of a class. Functions allow you to organize your code into independent pieces of functionality. They can be used to return various results based on input you provide. Perhaps most important, functions can encapsulate behaviors within a class and provide a public interface to those behaviors — an organized way for other code to access those behaviors. This chapter covers ways to use functions and create your own from scratch.

If there's one thing to remember about ActionScript, it's that every variable and part of a class is an object. Functions are no exception. While it might be strange to imagine, functions are instances of the Function class and contain their own methods and properties. In the section "Functions as Objects," you'll learn more about this.

Calling Functions

At the most basic level, a function is a well-defined piece of code, a sequence of statements in a block that has a beginning and an end. A function wraps up these sequences, and you run them at will. Every program you'll write will depend heavily on functions.

Executing the code within a function is known as calling or invoking the function. Functions in ActionScript are called by using the function's name followed by a pair of parentheses (). Officially, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required