- Data and Variables
- Expressions
- Functions and Methods
- Where to Learn More
Functions and Methods
Let’s now turn our attention to functions. Functions represent an important concept and can be considered the brains of a computer language. They are the organizers of computer languages as they group related script statements to perform a specific task.
In early programming, all code was executed as one block or one main routine. However, as programs got more complex, it became unwieldy to control such long blocks of code. In response to this growing complexity, longer blocks of code were broken up into more manageable sub-blocks called procedures, subroutines or (in the case of Flash) functions.
Procedures and functions were a way to group blocks of code whose execution was deferred until called upon by the main code routine. Think of ActionScript functions as a mini-program or collection of related code that performs a particular task when—but not before—it is called upon. This makes programs more efficient because the overhead required to execute the code is not expended until the program needs to execute the specific function task.
Programming that uses these procedures is called procedural programming. Procedural programming also has its limitations and has given way to an even more flexible method of constructing code called object-oriented programming. As I mentioned previously, ActionScript is an object-oriented programming language. Just as procedural programming incorporated and built upon unstructured coding, object-oriented programming also incorporates, and even requires, a lot of procedural programming. As such, functions are a vital part of ActionScript; you will use them in many of the scripts you construct.
Before you can use a function, you must create or define them. You can do this by using one of two possible syntaxes, described in the following sections.
Named and Anonymous Functions
The following code represents the most common way of creating a function in ActionScript; it is referred to as a named function. Notice that the function declaration begins with the word function, followed by the name of the function. The name can be anything of your choice as long as you follow the naming conventions supported by Flash, namely no keywords (words used and reserved by ActionScript for specific purposes), spaces, or strange characters (!@#$%^&*).
Function myfunction (parameter:DataType, parameter2:DataType,etc.){//actions go here; }
Following the function name is some code contained in parentheses. The parentheses can be blank or can contain parameters or arguments the function can use. If you leave the parameters blank you will have a function that performs the same way each time it is called. If you insert parameters, the function will perform a unique way depending on the parameter values. Providing the function with parameters is referred to as passing arguments.
Examples of named functions are the following:
function square(num){ Return num*num; Function error_message() Trace("This is an error message");
The other syntax is used to create anonymous functions. This syntax is as follows:
myfunction = function (parameter:DataType, parameter2:DataType,etc.){/*actions go here*/; }
The difference between the anonymous and the named functions is the way the function name is assigned. In the anonymous syntax, the function name appears first, and the syntax for defining the function follows the = assignment operator.
There are reasons for writing functions both ways. Use the anonymous function syntax to create a function dynamically. Named functions are available anywhere within their scope, no matter whether they are defined before or after they are invoked. Anonymous functions, on the other hand, must be defined before they can be invoked.
Examples of anonymous functions are as follows:
Square = function (num){ Return num*num; Error_message = function(){ Trace("This is an error message.");