actionscript 3.0 for journalists

The Anatomy of a Function

For many people who studied under ActionScript 2.0, functions might seem like one of the more drastic changes in ActionScript 3.0. However, they haven't actually changed at all, it is just that Flash is now more strict in the way they are typed out. But, before we even go that far, let take a look at how a function works.

It is best to think of a function as a list of instructions, like a recipe. It is a block of code that is independent from the rest of the program. It just sits there, waiting, until you call upon it to execute. Putting code into functions is a way to make your program modular. Each function can have its own task, and can be run whenever you need.

Structure of a function

First, a function requires the actual word function. This is a word reserved by Flash, and it will turn blue as soon as you type it. This tells Flash you are writing a new function.

function

Next, you need a name for your function. This name follows all of the standard conventions for naming variables. No spaces; use only numbers, letters and underscores; must start with a letter and standard practice is that function names are always lowercase.

function showMessage

Next we need a set of parenthesis. These parentheses will serve a purpose in one of the next steps, but for now, we will place them there as required by Flash.

function showMessage()

The next part is specifying the boundary of the code that will be included in the function. We use the curly brackets on the keyboard: { }, not to be confused with the straight brackets. The first bracket is the opening of the function, and the second one is where the function closes. Any code between the brackets is what is contained within the function.

function showMessage(){

  }

Notice that functions do not need semicolons. They are one exception to the rule. However, all of the code contained within a function needs a semicolon after each line, to end each statement.

Now let's make a working example. First we will put some code into this function.

function showMessage(){
       trace("Hello World"); 
}

If we were to run this now (and feel free to try it), we would not see the message in our output window. Why? The reason is because a function does not run until you tell it to. It is nothing more than a block of code that we identify with a name. Once we are ready, we can run the function by calling it with some additional code.

Try adding the line of code showMessage() below the function. This is what we use to run this function.

function showMessage(){
      trace("Hello World"); 
}

showMessage();

A basic function in Flash

In our output window, we will see the trace command in action. Any time we want to run a function, we can simply type in the name and add a set of parenthesis.

Using function parameters

This is usually the step that most people get lost. Spend some time trying to understand how function parameters work. They become a valuable tool in advanced ActionScript coding.

Remember that parenthesis we left blank? That is actually a space for something called a parameter. We can actually put a variable in that space that can pass information to our function:

function showMessage(theMessage:String){
      trace("Hello World");
 }

showMessage();

This new theMessage variable we have between the parenthesis is called a local variable. It means this variable is unique in a way that it only exists within the scope of this function, and it is temporary. We do not need the var command here, but we do need to let Flash know the type of data this variable can use.

Now we can use this temporary variable within our function:

function showMessage(theMessage:String){
      trace(theMessage);
 }

 showMessage();

If you tried running the program now, you would get an error that said you have an incorrect number of arguments, and that Flash expected one but you gave it none. That appears because when we call the function, we need to include some information to send to this parameter. Inside the parenthesis of the showMessage function call is a space for something called an argument. An argument is some data that is sent to a function parameter. In our last line of code, we're going to put in a string of text inside the parenthesis.

function showMessage(theMessage:String){
      trace(theMessage);
 }

 showMessage("Sending data to my function");

When we call this function using the last line of the code, we are sending a string of text to the function. That string of text is being stored in theMessage variable, and used within the function. Why would we do this? It allows us to make our function reusable. Try calling the function several times, sending different strings of text:

Using function arguments to send data

Specifying return type void on functions

There is a feature of functions that is rarely used by most beginners, and that is having a function return some data. In more complex situations, you can have your function return some data back to the section of code that called it. For example, imagine if someone wrote a calculator function. You could send that function two numbers, and the function would say add those two numbers, then return the answer back to the part of the code that called it.

We are not going to write those types of functions in this tutorial, but in ActionScript 3.0, standard convention is to specify a something called a return type. Essentially, what we are going to do is tell Flash that our functions will not return any data and not worry about it. To do this we add :void just after the parentheses of every function we write. Probably 99 percent of the functions we will write will use this same void datatype that tells Flash that our function will not return any data, but it is an important part of ActionScript 3.0.

Specifying a void return type on Flash function