Beginning ActionScript 3.0 – Default Parameter Values

soft-drink-256x256 After the talking about how to define the functions, and calling them, we are going to talk about the parameters. And today’s topic is about the default parameter values.

Default parameter values

In some other programming, you can designate the default values to some parameters so that when you call the functions, you can ignore the parameters which have default values, and the parameters will use the default values inside the function.

This mechanism is also supported by ActionScript 3.0.

In ActionScript 3.0, you can declare default parameter values for a function. If a call to a function with default parameter values omits a parameter with default values, the value specified in the function definition for that parameter is used. All parameters with default values must be placed at the end of the parameter list. The values assigned as default values must be compile-time constants. The existence of a default value for a parameter effectively makes that parameter an optional parameter. A parameter without a default value is considered a required parameter.

For example, we can define the function in this way:

function sayHello(somebody:String = “world”):void {
trace(”hello, “+somebody);

And we can call the function with or with the parameter, such as:


And the output will be:


When you using the default values with formal parameters, you should remember that the default values should be known in the compile-time, and the parameter with default values should be placed at the end of the parameter list.

So, if you using the default values this way:

function sayHello(somebody:String = “world”, someValue:int):void {
trace(”hello, “+somebody);

You’ll get a compiler error.


And this way:

function getRandomNumber(randomNumber:Number = Math.random()):Number {
return randomNumber;

also won’t work.


Because the function Math.random() can’t be determined at the compile-time, it lies on the machine time.

If you want to return the random number inside your function, you can change your function into:

function getRandomNumber():Number {
return Math.random();


All the return values of function are not compile-time constants. So, don’t use this kind of default value.

Another thing you need to notice is that, when you have two or more parameters with default values, once one of them are designated, the other parameters before it should be designated too.

Suppose we define a function like follows:

function traceNumber(x:int, y:int = 2, z:int = 3):void {
trace(x, y, z);

Of course, we can call the function by this statement:


but we can’t use the following statement:

traceNumber(1, ,4);

You should designate the second parameter here.

OK, much for this topic :)

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