Recall the generic syntax of function invocation with no arguments:
funcName( )
To supply (or pass) arguments to a function, we
provide a list of values within the parentheses when invoking the
function:
funcName(arg1, arg2, arg3,...argn)
The values used as arguments may be any legitimate expression in
ActionScript, including compound expressions. For example, earlier we
defined a simple function, say( ), that expects
a single parameter, msg:
function say(msg) {
trace("The message is " + msg);
}
To invoke say( ), we use a statement like this:
say("This is my first argument...how touching");
Or like this:
say(99);
Notice that the values, "This is my first argument . . . how
touching" and 99, belong to different datatypes. ActionScript
allows us to pass any data of any type to a function, as long as the
function knows what to do with the passed value. (Languages such as C
require that we predefine the datatype for each parameter, and they
display an error if data of the wrong type is provided.)
Before each argument is passed, its value is fully resolved. We may,
therefore, also invoke functions with complex expressions. For
example:
var name = "Gula";
say("Welcome to my web site " + name);
Because the expression "Welcome to my web site " +
name is evaluated before it is passed to the
say( ) function, the function receives the
resolved value "Welcome to my web site Gula". This means
that we can programmatically generate function arguments. Powerful
stuff.
Each of the three arguments is assigned as the value of the
corresponding parameter named in the function declaration:
ball is assigned to theClip, 5
is assigned to xDist, and -15 is assigned to
yDist. We can move any clip any distance by
invoking our generic moveClip( ) function with
different arguments. Here we move the clip instance named
square to the right 2 pixels and down 100
pixels: