Adobe 38043740 - ColdFusion Standard - Mac Development Manual page 166

Developing applications
Hide thumbs Also See for 38043740 - ColdFusion Standard - Mac:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Building Blocks of ColdFusion Applications
This solution is simple, but it is not always optimal:
• This technique requires ColdFusion to copy the entire array twice, once when you call the function and once when
the function returns. Doing so is inefficient for large arrays and can reduce performance, particularly if the function
is called frequently.
• You can use the return value for other purposes, such as a status variable.
If you do not use the
return
structure and change the array values inside the structure. Then the calling page can access the changed data by using
the structure variable it passed to the UDF.
The following code shows how to rewrite the previous example using an array in a structure. It returns True as a status
indicator to the calling page and uses the structure to pass the array data back to the calling page.
<cfscript>
//Initialize some variables.
//This creates a simple array as an element in a structure.
arrayStruct=StructNew();
arrayStruct.Array=ArrayNew(1);
arrayStruct.Array[1]=2;
arrayStruct.Array[2]=22;
//Define the function.
function doubleOneDArrayS(OneDArrayStruct) {
var i = 0;
for ( i = 1; i LE arrayLen(OneDArrayStruct.Array); i = i + 1)
{ OneDArrayStruct.Array[i] = OneDArrayStruct.Array[i] * 2; }
return True;
}
//Call the function.
Status = doubleOneDArrayS(arrayStruct);
WriteOutput("Status: " & Status);
</cfscript>
</br>
<cfdump var="#arrayStruct#">
Use the same structure element name for the array (in this case Array) in the calling page and the function.
About the Arguments scope
All function arguments exist in their own scope, the Arguments scope.
The Arguments scope exists for the life of a function call. When the function returns, the scope and its variables are
destroyed.
However, destroying the Argument scope does not destroy variables, such as structures or query objects, that
ColdFusion passes to the function by reference. The variables on the calling page that you use as function arguments
continue to exist; if the function changes the argument value, the variable in the calling page reflects the changed value.
The Arguments scope is special, in that you can treat the scope as either an array or a structure. This dual nature of the
Arguments scope is useful because it makes it easy to use arguments in any of the following circumstances:
• You define the function using CFScript.
• You define the function using the
• You pass arguments using argument name=value format.
• You pass arguments as values only.
• The function takes optional, undeclared arguments.
statement to return the array to the caller, you can pass the array as an element in a
tag.
cffunction
Last updated 1/20/2012
161

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion 9

Table of Contents