Adobe 38043740 - ColdFusion Standard - Mac Development Manual page 173

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

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Building Blocks of ColdFusion Applications
<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>
Reviewing the code
The following table describes the code that has been changed or added to the previous version of this example. For a
description of the initial code, see
Code
function TotalInterest(principal, annualPercent, months, status)
if ((principal LE 0) OR (annualPercent LE 0) OR (months LE 0)) {
Status.errorMsg = "All values must be greater than 0";
Return -1;
}
Calling the function
The code that calls the function now looks like the following. Code that is changed from the example in
defined function
example" on page 171 is in bold.
"A user-defined function
example" on page 171.
Last updated 1/20/2012
Description
The function now takes an additional
argument, a status structure. Uses a
structure for the status variable so that
changes that the function makes affect the
status structure in the caller.
Checks to make sure the principal, percent
rate, and duration are all greater than zero.
If any is not, sets the errorMsg key (the only
key) in the Status structure to a descriptive
string. Also, returns -1 to the caller and
exits the function without processing
further.
"A user-
168

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion 9

Table of Contents