Beginning ActionScript 3.0 – Parameter Passing

mouse-48x48 OK, after some talking about the parameters, let’s go a little deeper with the parameters. This section is about the mechanism of passing the parameters.

Complex value

In the printScore function, we declare a variable named subjectArray with Array type as parameter.

function printScore(subjectArray:Array)…

Here, the subjectArray is called formal parameter. All the variables appear in the parenthesis are formal parameters.

In the calling statement, such as:




The “math” or “music” here is called actual parameter.

We’ve already know that when we call the functions, the program flow will jump out from the current state into the function entry. Before the program flow wants to jump to the function entry, the runtime environment will allocate the space in the memory for the function execution. Eh, the space can be use for save the actual parameter, return address (often points to the next statement after function calling statement) and so on.

Let’s focus on the parameters, before calling the function, printScore(math), the memory layout maybe as follows:


Then, the program flow meets the function calling statement, so the runtime environment allocate the space for the function execution, then the actual parameter, math, will be push into the function stack. Eh, to be exactly, the value of math, the reference of the Array will be push into the stack, and this reference will became the value of the formal parameter.

So, the memory layout will be:


Now, you should know that we use the subjectArray in the function outputs the content of math Array.

for( var i:int = 0; i < subjectArray.length; i++) {
trace(i + “:” + subjectArray[i]);

Primitive value

OK, show you another example now.

function swap(a:int, b:int):void {

a = a^b;
b = a^b;
a = a^b;

trace(”inside the functoin:”);
trace(”a : ” + a);
trace(”b : ” + b);

var numberA:int = 1;
var numberB:int = 2;

swap(numberA, numberB);

trace(”outside the function”);
trace(”numberA : ” + numberA);
trace(”numberB : ” + numberB);

We define a function for swapping the values of two integers named swap. Then we define two int type value, numberA and numberB, and call the function with numberA and numberB. Further more, we use trace statement to output the values inside the function and after the function execution.

Eh, guess the output J

At last the value of numberA is 2, or not? And the value of numberB is 1, or not?

Let’s see the output.


Wow, it seems that the values of the formal parameters did changed, while the values of numberA and numberB haven’t got changed.

According to our conclusion above, the formal parameter refers to the same value as the actual parameter. But in this case, it seems not.

What’s the problem?

Actually, that’s all because the int type is a primitive type. Still remember our discussion about the difference about the primitive value and complex value?

When we pass the numberA and numberB into the swap function, the formal parameter “a” refers to the same value object of numberA refers to, and so does “b” and numberB. But, when you change the value of “a”, then “a” refers to another value object, no longer refers to the value object of numberA refers to. So, you change the value of “a” or “b” won’t affect the value of numberA or numberB.

And for the complex value, you should know the reason, isn’t it?


Someone says for the primitive value, you can consider the parameters are passing by value; for the complex value, you can consider the parameters are passing by reference. Passing by value or reference is a compiler mechanism.

If you know the mechanism of passing by value or reference, remember it, and you don’t need to read the following text. if not, the following text may helps you.

When calling the functions, we can simply consider that the actual parameter takes the place of formal parameter.

For example, when we use


we can consider that there is an assignment statement like:

subjectArray = math;

Then, the formal parameter subjectArray is refers to the actual parameter math. If you change the content in subjectArray, the math Array will also be affected.

If you use the calling statement:

swap(numberA, numberB),

There still exist the assignments:

a = numberA;
b = numberB;

When you do some operations on a or b, and want to change the value of a or b, numberA or numberB won’t be affected, due to the mechanism of storing the primitive value object in ActionScript.

My suggestion is that, you just need to remember there exist assignments when you calling the function. The difference between primitive value and complex value is due to the different storing mechanisms of different type.

Share and Enjoy:
  • Digg
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

Leave a Reply