Chapter 4. Exploring ActionScript Functions and Scope
Several critical aspects of programming include reusability, organization, and understandability. Reusability enables you to take a section of code and to apply it in numerous places within your program and within other programs. Organization allows code to be found easily, and to have different sections of code relate in ways that make sense. Understandability means that your code still makes sense when you return to it after several weeks.
This chapter introduces you to ways of organizing your code so that it is logically structured, easy to read, and easy to reuse.
Functions
The code snippets used in this book so far have been quite short, and have been written in sort of a stream-of-thought style. As programs get larger, code reusability, organization, and understandability become essential.
The first step toward this goal is the proper use of functions. A function is a reusable grouping of code that performs a single task. A function may require data to be passed in as arguments, and a function may return data as a result of some computation.
Say that you need to write some code that includes formatting an email address, and that several places within the code need such formatting. Rather than duplicating the code that is responsible for the formatting, you write it once in a function. Wherever an email address needs to be formatted in your program, the function is called to handle it. The function acts as a reusable container ...
Get Beginning ActionScript 2.0 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.