Adobe COLDFUSION 9 Manual page 165

Developing applications
Hide thumbs Also See for COLDFUSION 9:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Building Blocks of ColdFusion Applications
• 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
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, always handle arrays
differently from all other complex data types.
Passing structures, queries, and objects
For your function to modify the copy of a structure, query, or object, in the caller, pass the variable as an argument.
Because the function gets a reference to the structure in the caller, 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 copy of a structure, query, or object, in the caller, use the
function to make a copy and pass the copy to the function.
Passing arrays
If you want your function to modify the caller's 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
variable name in the function argument and return variable.
The following example shows how to directly pass and return arrays. In this example, the
doubles the value of each element in a one-dimensional array.
<cfscript>
//Initialize some variables
//This creates a simple array.
a=ArrayNew(1);
a[1]=2;
a[2]=22;
//Define the function.
function doubleOneDArray(OneDArray) {
var i = 0;
for ( i = 1; i LE arrayLen(OneDArray); i = i + 1)
{ OneDArray[i] = OneDArray[i] * 2; }
return OneDArray;
}
//Call the function.
a = doubleOneDArray(a);
</cfscript>
<cfdump var="#a#">
"Passing complex
return
Last updated 8/5/2010
data" on page 160.
Duplicate
statement. In the caller, use the same
doubleOneDArray
160
function

Advertisement

Table of Contents
loading

Table of Contents