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, ...
Get ActionScript 3.0 Bible 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.