Chapter 4. Working with Methods and Functions


In this chapter

  • Reusing blocks of code by defining your own functions

  • Calling functions and passing arguments using the call operator

  • Getting results by returning values from a function

  • Working with functions as objects

  • Creating recursive functions

Now that you know all about creating variables, you probably want to start actually doing something with them. This is where methods and functions come in. Functions are reusable blocks of code that can be defined in your own classes and are frequently used throughout the ActionScript 3.0 API.

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 and functionality within a class and provide a public interface to that functionality. 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 this chapter, we talk about how functions can be used as objects.

Understanding Functions

At the most basic level, a function is a piece of code that has been stored as a saved routine that can be run at will by writing out the function's name or calling the function. ...

Get ActionScript™ 3.0 Bible now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.