Beginning ActionScript 3.0 – Calling Functions


Question piled upon a question. After we know how to define the functions, we need to know how to use it.




Calling functions

Remember the identifier that we declare in the function statement? Now, you can use the identifier to call the function.

In ActionScript, you call a function by using its identifier followed by the parentheses operator (()). You use the parentheses operator to enclose any function parameters you want to send to the function.

Turn back to our example code, and focus on the center section.

  1. trace(”============Mathematics==========”);
  2. printScore(math);
  3. trace(”============History==========”);
  4. printScore(history);
  5. trace(”============Music==========”);
  6. printScore(music);

In this code, we use the printScore() three times with different parameters, the first time is math, then history and the third is music.

The statement:

  1. printScore(math);

means we call the printScore function here, and pass the math variable as parameter to the printScore function.

After you know how to call the functions in your code, there is also something you need to notice when you call the function.

The first thing is keep the number of parameters the same with the function definition. That means if you declare one parameter, you should pass one parameter when you call the function. If you use two or more parameters, or you don’t pass any parameters, you’ll get an error from the compiler.

For example, if we call the printScore function without parameters, we’ll get the following result.


The second thing is about the data type.

Suppose we define a function to join two strings like the following:

  1. function concat(stringA:String, stringB:String):String {
  2. return stringA+stringB;
  3. }

Now, we declare two formal parameters, stringA and stringB, and both of them are String type. So, if we use the following calling statement:

  1. concate(1,2);

we’ll get compiler errors.


Because, we use type int values inside the calling statements, while the function definition says they should be String type. So, when you calling functions, remember use the same type with the formal parameters.

In this example, we can change into:

  1. concate(”1″,”2″)

The third one is about the order.

Eh, when you declare two or more formal parameters inside your function, please remember the order, which means when you using the calling statements you should keep the parameters the same order with the definition.

For example, if you declare a function like this:

  1. function concate(integerA:int, stringB:String):String {
  2. return integerA.toString()+stringB;
  3. }

Then, in the calling statement, you should keep the parameters in order, type int should be first, followed by the string type value.

  1. concate(1,”2″)

Remember, the compiler is not so smart. The compiler will check the parameters one by one. So, you need to keep the same number, the same type, and the same order J