Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.


AS3: Where are function variables declared?

Posted October 20, 2008 by senocular

At the top! Yes, well, maybe you *put* them closer to the bottom, but Flash still declares them at the top of the function. Confused? Lets take a look at some examples.
function myFunction(){
trace(i); // what is i?
var i:int = 10;

What's traced? Were you going to say 10? I hope not; and nope, the answer is not undefined either. The answer is 0. Why? Because variables are declared (not defined) at the top of a function block. Declarations do not include a definition. Here, since the variable is an integer (int) the default value is 0. For this example, the variable i has been declared for the trace, but the value is not assigned (to 10) until after it.

In essence, the above function in practice becomes more like:

function myFunction(){
var i:int;
i = 10;

This applies to all variables in a function block, no matter what other nested block (if, for, etc.) they're contained. All declarations are implicitly moved to the head of the function.

Lets take a look at another example:

function myFunction(){
var x:int;
while (x++ < 10){
var y:int;
while(y++ < 10){
trace(x, y);

What is traced? The answer:

1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10

And that's it; no more. One might think that much more data would be produced for each x and y loop. But what happens is that the dependency on having y default to 0 is lost since that declaration is moved to the top of the function. So instead of y resetting to 0 in each x loop iteration, nothing happens meaning the y loop (with the trace) only gets called for the first iteration of x and no more. The solution is to explicitly set y to 0 within the x loop.

var y:int = 0;

There are a couple of implications as a result of this behavior. First, as seen above, you will need to be much more cautious of default variable values. Defaults will only be set once and only at the top of the function. Though, for the most part, the default value will persist, it is not the case with loops as seen in the last example.

Secondly, as you might have already noticed with existing compiler warnings, you'll have to be sure you avoid duplicate variable declarations in the same function block. Even if in your implementation there's no way for those variables to cause a conflict, given the way Flash assumes declaration control, there very well could end up being one.

Lastly, and this is on a more positive note, your fear of including variable declarations inside of loops (because as we all know, it was a bad idea for performance in AS2) is no longer valid. Since Flash automatically pulls those declarations out of their original context and places them in the head of the function, there are no performance-related consequences to having them inside of loops.

In any case, it's good to know that this kind of behavior exists since it could help explain some confusing situations you might find yourself getting into.