User Guide

256 Chapter 13 Extending ColdFusion Pages with CFML Scripting
function makes using the structure appears, but the change the function makes
using the directly passed array does not affect the array outside the function.
<CFScript>
//Create a two-element array inside a structure
mystruct = StructNew();
mystruct.myarray = ArrayNew(1);;
mystruct.myarray[1] = "This is the original element one";
mystruct.myarray[2] = "This is the original element two";
//Define a custom function to manipulate both
//an array in a structure (using struct_param) and
//an array that is passed directly (using array_param).
function setarray( struct_param, array_param )
{
//Change the first element of the array passed in the structure
struct_param.myarray[1] = "This is the NEW element one";
//Change the seond element of the directly-passed array
array_param[2] = "This is the NEW element two";
return "success";
}
//Call the function passing the structure and the array
setarray( mystruct, mystruct.myarray);
</CFScript>
<CFOutput>
<!--- The element one is changed --->
<br>#mystruct.myarray[1]#
<!--- Element two is unchanged because the function got a copy --->
<br>#mystruct.myarray[2]#
</CFoutput>
Error handling
You can handle errors in custom functions by writing a status variable indicating
success or failure and some information about the failure. You can also return a
special value to indicate failure. The following sketch outlines possible combinations
of both these approaches:
function bigCalc(x, y, errorInfo)
{
// Clear error state
// This allows errorInfo struct to be reused
structClear(errorInfo);
var isOK = true;
// Do work, populate fields in errorInfo such as
// errorNumber, errorMsg, errorDetail, whatever
...
if (isOK)
{
return calculatedValue;