User Guide

Table Of Contents
Working with arguments and variables in functions 179
Passing arguments
ColdFusion passes the following data types to the function by value:
Integers
Real numbers
Strings (including lists)
Date-time objects
Arrays
As a result, any changes that you make in the function to these arguments do not affect the
variable that was used to call the function, even if the calling code is on the same ColdFusion page
as the function definition.
ColdFusion passes queries, structures, and external objects such as COM objects into the
function by reference. As a result, any changes to these arguments in the function also change the
value of the variable in the calling code.
For an example of the effects of passing arguments, see “Passing complex data” on page 179.
Passing complex data
Structures, queries, and complex objects such as COM objects are passed to UDFs by reference,
so the function uses the same copy of the data as the caller. Arrays are passed to user-defined
functions by value, so the function gets a new copy of the array data and the array in the calling
page is unchanged by the function. As a result, you must handle arrays differently from all other
complex data types.
Passing structures, queries, and objects
For your function to modify the caller’s copy of a structure, query, or object, you must pass the
variable as an argument. Because the function gets a reference to the caller’s structure, the caller
variable reflects all changes in the function. You do not have to return the structure to the caller.
After the function returns, the calling page accesses the changed data by using the structure
variable that it passed to the function.
If you do not want a function to modify the callers copy of a structure, query, or object, use the
Duplicate function to make a copy and pass the copy to the function.
Passing arrays
If you want your function to modify the callers copy of the array, the simplest solution is to pass
the array to the function and return the changed array to the caller in the function
return
statement. In the caller, use the same variable name in the function argument and return variable.
The following example shows how to directly pass and return arrays. In this example, the
doubleOneDArray function doubles the value of each element in a one-dimensional array.
<cfscript>
//Initialize some variables
//This creates a simple array.
a=ArrayNew(1);