
214
Chapter 10: Writing and Calling User-Defined Functions
Providing status information
In some cases, such as those where the function cannot provide a corrective action, the function
cannot, or should not, handle the error directly. In these cases, your function can return
information to the calling page. The calling page must handle the error information and act
appropriately.
Consider the following mechanisms for providing status information:
•
Use the return value to indicate the function status only. The return value can be a Boolean
success/failure indicator. The return value can also be a status code, for example where 1
indicates success, and various failure types are assigned known numbers. With this method, the
function must set a variable in the caller to the value of a successful result.
•
Set a status variable that is available to the caller (not the return variable) to indicate success or
failure and any information about the failure. With this method, the function can return the
result directly to the caller. In this method, the function should use only the return value and
structure arguments to pass the status back to the caller.
Each of these methods can have variants, and each has advantages and disadvantages. Which
technique you use should depend on the type of function, the application in which you use it,
and your coding style.
The following example, which modifies the function used in
“A User-defined function example”
on page 204
, uses one version of the status variable method. It provides two forms of error
information:
•
It returns -1, instead of an interest value, if it encounters an error. This value can serve as an
error indicator because you never pay negative interest on a loan.
•
It also writes an error message to a structure that contains an error description variable. Because
the message is in a structure, it is available to both the calling page and the function.
The TotalInterest function
After changes to handle errors, the
TotalInterest
function looks like the following. Code that is
changed from the example in
“A User-defined function example” on page 204
is in bold.
<cfscript>
function TotalInterest(principal, annualPercent, months, status) {
Var years = 0;
Var interestRate = 0;
Var totalInterest = 0;
principal = trim(principal);
principal = REReplace(principal,"[\$,]","","ALL");
annualPercent = Replace(annualPercent,"%","","ALL");
if ((principal LE 0) OR (annualPercent LE 0) OR (months LE 0)) {
Status.errorMsg = "All values must be greater than 0";
Return -1;
}
interestRate = annualPercent / 100;
years = months / 12;
totalInterest = principal*(((1+ interestRate)^years)-1);
Return DollarFormat(totalInterest);
}
</cfscript>
Summary of Contents for ColdFusion MX
Page 1: ...Developing ColdFusion MX Applications...
Page 22: ...22 Contents...
Page 38: ......
Page 52: ...52 Chapter 2 Elements of CFML...
Page 162: ......
Page 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Page 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Page 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Page 266: ......
Page 314: ...314 Chapter 14 Handling Errors...
Page 344: ...344 Chapter 15 Using Persistent Data and Locking...
Page 349: ...About user security 349...
Page 357: ...Security scenarios 357...
Page 370: ...370 Chapter 16 Securing Applications...
Page 388: ...388 Chapter 17 Developing Globalized Applications...
Page 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Page 410: ......
Page 426: ...426 Chapter 19 Introduction to Databases and SQL...
Page 476: ...476 Chapter 22 Using Query of Queries...
Page 534: ...534 Chapter 24 Building a Search Interface...
Page 556: ...556 Chapter 25 Using Verity Search Expressions...
Page 558: ......
Page 582: ...582 Chapter 26 Retrieving and Formatting Data...
Page 668: ......
Page 734: ...734 Chapter 32 Using Web Services...
Page 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Page 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Page 788: ......