What is a function and how it is implemented in ActionScript 2.0. A function is an operation that Flash must execute each time it is called.

In order to execute that operation, you must first tell FLASH three things:

            1-what is the name of the function

            2-how to do the operation

            3-call the function when needed

A function is something like somebody that you have trained to do a specific task (like for example cleaning the dishes), and when you call him, he knows that your dishes need to be cleaned; it’s just a stupid example, of course this person doesn’t exist!

             Let’s give an example of a FLASH function: suppose you want FLASH to execute an addition of two numbers a and b. As said earlier, we need to do three things:

1.Give a name to the function:

We could call it a fancy name like abracadabra and it would work but if I want to remember quickly what this function does, I’d rather give it an expressive name; what do you think of “addab? Pretty obvious, isn’t it?

In FLASH language ,called ActionScript, one way of giving a function a name is just writing  this in the ActionScript pannel:

function addab (a,b)

It just tells FLASH that the word after the word function is the name of the function; notice that the word function must be written exactly like it is, otherwise FLASH won’t recognize it. For example don’t write it with capitals!

In programming language, the word function is called a keyword  ; notice that it becomes blue when it is written without mistake, so avoid giving variables a name like that, otherwise FLASH will be confused!

             The letters a and b between brackets are called arguments; it just tells FLASH that the operation that we want him to do is going to be applied to the variables a and b.

2. Define the operation

             Well, programming is no more than teaching a computer to do things; it is not as smart as you (see above, you , for example, you know that an operation name with small or capital letters is probably the same! The computer doesn’t!).

Further more, if somebody gives you two numbers a and b and then tells you “addab”, you’ll probably guess that he wants you to add the two numbers! Unfortunately, for the computer, you have to tell it exactly what to do with the two numbers!

Here is how:

var a:Number;

var b:Number;

var s:Number;

function addab(a,b)

{

s=a+b;

}

The first two lines tell FLASH that the variables a and b are numbers (to you, it is obvious that you can add only numbers, but for the computer, it is not; these two variables could be strings like somebody's name and he would be rather “embarrassed” to add them…).

The third line tells to FLASH where we are going to store the result of the operation, that’s to say the sum; that’s why I called it s (you could call it whatever you want like sum, mysum, etc…anything except the keywords we talked about above).

Then there is a line where we open a curly bracket; it is the way we tell FLASH where the operation begins; usually, this curly bracket is placed in the same line after the argument, but I prefer to place it underneath; the reason why is that you have to close your function with another curly bracket, and believe me, 50% of the time you forget); this way, it is very clear and fast to check: for every open curly bracket, there’s got to be a close curly bracket after it.

When you are learning a programming language (FLASH is no exception!), you have to develop your own tricks to simplify your work (yes, programming is not always clear…).

3. Call the function

It’s time to call our function and make it work; once we have done all the job above (I know you're complaining it's a lot), all we have to do to make it work is to call its name (it sounds like a certain song...).

The name is not enough, we have to add the argument (the two numbers that we want to add, let’s say for example 12 and 5):

 addab(12,5);

FLASH will know exactly what to do! (if it could exist something like that for the dishes!).

TEST OF OUR FUNCTION 

It's time to try our function; open a new Flash document and double-click on the first layer to call it Actions.

Then click on the first frame and open the Actions pannel with the top menu (Window/Actions); write our code (or select it and paste it):

var a:Number;
var b:Number;
var s:Number;
function addab(a,b)
{
s=a+b;
}
addab(12,5);
trace(s)

Notice that I added a line [ trace(s)] that just tells FLASH to write the result of our operation in the output window so we can check our result (I suppose you know the result!).

Now play your movie (with the top menu Control/Test Movie) and you should see in the output window the result 17.

Try other numbers in the arguments to veryfie that it works with any number. Try also to write a function that adds 3 numbers: I'm sure you can! Just apply what we have done above!

I think now you know what a function is!

In this tutorial we gave an example for adding two numbers (it’s not much fun), but a function in FLASH can be much more fun: for example we can use a function to move an object on the stage randomly or move it with the keypad arrow keys. That's what we'll see in our next tutorial. Thank you for your attention!