User Guide
252 Chapter 13 Extending ColdFusion Pages with CFML Scripting
Each function has a built-in Arguments array containing all arguments passed to the
function: the required arguments specified by the function parameters followed by
any additional arguments included in the function call.
The function can determine the number of arguments passed to it by using the
ColdFusion function call
ArrayLen(Arguments).
The function must retrieve the optional arguments by using the
Arguments array. For
example, if the following function:
function MyFunction(MyArg1, MyArg2)
has three optional arguments, you can refer to the first two, required, arguments as
MyArg1 and MyArg2 or as Arguments[1] and Arguments[2]. You must refer to the
third and fourth and fifth, optional, arguments as
Arguments[3], Arguments[4],
and Arguments[5].
However, you must be careful if you mix references to the same argument using both
its parameter name and its place in the
Arguments array. Mixing is fine if all you do is
read the argument. If you write the argument, you should consistently use either the
parameter name or the Arguments array.
Passing arguments
ColdFusion passes arrays and simple data types including integers, strings, and time
and date values into the function by value. It passes queries and structures into the
function by reference. As a result, if you change the value of a query or structure
argument variable in the function, it changes the value of the variable that the calling
page used in calling the function. However, if you change the value of an array or
string argument variable in the function, it does not change the value of the string
variable in the calling page.
Using variables
A function can access all variables that are available in the calling page. In addition,
the function has its own private scope that contains the function parameters, the
Arguments array, and the var-declared variables. This scope is only accessible inside
the current instance of the function. As soon as the function exits, all the variables
are removed.
A function can use and change any variable that is available in the calling page,
including variables in the caller’s Variables (local) scope, as if the function were part
of the calling page. For example, if you know that the calling page has a local variable
called Customer_name (and there is no
var variable named Customer_name) the
function can read and change the variable by referring to it as "Customer_name" or
(using better coding practice) "Variables.Customer_name".
Similarly, you can create a local variable inside a function and then refer to it
anywhere in the calling page after the function call. You cannot refer to the variable
before you call the function.
Because function var variables do not take a scope identifier and exist only while the
function executes, function variable names can be independent of variable names