Function Parameters Revisited

Now that we’re comfortable with how functions work, let’s return to the topic of function parameters. Our current discussion requires a little knowledge of objects, so new programmers may want to read Chapter 12 before reading this section.

Number of Parameters

Earlier we learned that the parameters of a function are declared when the function is created. Recall the syntax:

function funcName (param1, param2, param3,...paramn) {

Perhaps surprisingly, the number of parameters passed to a function can differ from the number specified in the formal function declaration. Functions can accept any number of parameters, whether more than or fewer than the “expected” number. When a function is called with fewer than the declared number of parameters, the value of each missing parameter is set to undefined. For example:

function viewVars (x, y, z) {
  trace ("x is " + x);
  trace ("y is " + y);
  trace ("z is " + z);

viewVars(10);  // Displays: "x is 10", "y is ", and "z is " because 
               // y and z are undefined (and display as blanks)

When a function is called with more parameters than the declared number, excess parameter values can be accessed using the arguments object. (Obviously the excess parameters can’t be accessed by name like explicitly declared parameters because their names were not declared.)

The arguments Object

During the execution of any function, the built-in arguments object gives us access to three pieces of information: (a) the number of parameters ...

Get ActionScript: The Definitive Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.