Whatever message this page gives is out now! Go check it out!
<p>Enter your Name:
<input name="name" type="text" hspace="30" maxlength="30">
<input type="Submit" name="submit" value="OK">
</cfform>
<cfscript>
function HelloFriend(Name) {
if (Name is "") WriteOutput("You forgot your name!");
else WriteOutput("Hello " & name &"!");
return "";
}
if (IsDefined("Form.submit")) HelloFriend(Form.name);
</cfscript>| Code | Description |
<p>Enter your Name: <input name="name" type="text" hspace="30" maxlength="30"> <input type="Submit" name="submit" value="OK"> </cfform> | Creates a simple form requesting you to enter your name.Uses the script_name CGI variable to post to this page without specifying a URL. If you do not enter a name, the form posts an empty string as the name field. |
function HelloFriend(Name) { if (Name is "") WriteOutput("You forgot your name!"); else WriteOutput("Hello " & name &"!"); return ""; } if (IsDefined("Form.submit")) HelloFriend(Form.name); </cfscript> | Defines a function to display "Hello name!" First, checks whether the argument is an empty string. If so, displays an error message. Otherwise displays the hello message. Returns the empty string. (The caller does not use the return value). It is not necessary to use curly brackets around the if or else statement bodies because they are single statements.If this page has been called by submitting the form, calls the HelloFriend function. Otherwise, the page just displays the form. |
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>| Code | Description |
function TotalInterest(principal, annualPercent, months, status) | 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. |
Status.errorMsg = "All values must be greater than 0"; Return -1; } | 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. |
<cfset myInterest = TotalInterest(Form.Principal,
Form.AnnualPercent,Form.Months, status)>
<cfif myInterest EQ -1>
<cfoutput>
ERROR: #status.errorMsg#<br>
</cfoutput>
<cfelse>
<cfoutput>
Loan amount: #Form.Principal#<br>
Annual percentage rate:
#Form.AnnualPercent#<br>
Loan duration: #Form.Months# months<br>
TOTAL INTEREST: #myInterest#<br>
</cfoutput>
</cfif>| Code | Description |
<cfset status = StructNew()> | Creates a structure to hold the function status. |
(Form.Principal, Form.AnnualPercent, Form.Months, status)> | Calls the function. This time, the function requires four arguments, including the status variable. |
<cfoutput> ERROR: #status.errorMsg#<br> </cfoutput> | If the function returns -1, there must be an error. Displays the message that the function placed in the status.errorMsg structure key. |
<cfoutput> Loan amount: #Form.Principal#<br> Annual percentage rate: #Form.AnnualPercent#<br> Loan duration: #Form.Months# months<br> TOTAL INTEREST: #myInterst#<br> </cfoutput> </cfif> | If the function does not return -1, it returns an interest value. Displays the input values and the function return value. |
<cfargument name="principal" required="Yes">
<cfargument name="annualPercent" required="Yes">
<cfargument name="months" required="Yes">
<cfset var years = 0>
<cfset var interestRate = 0>
<cfset var totalInterest = 0>
<cfset principal = trim(principal)>
<cfset principal = REReplace(principal,"[\$,]","","ALL")>
<cfset annualPercent = Replace(annualPercent,"%","","ALL")>
<cfif ((principal LE 0) OR (annualPercent LE 0) OR (months LE 0))>
<cfthrow type="InvalidData" message="All values must be greater than 0.">
</cfif>
<cfset interestRate = annualPercent / 100>
<cfset years = months / 12>
<cfset totalInterest = principal*
(((1+ interestRate)^years)-1)>
<cfreturn DollarFormat(totalInterest)>
</cffunction><cfset status = StructNew()>
<cfset myInterest = TotalInterest(Form.Principal, Form.AnnualPercent,
Form.Months, status)>
<cfoutput>
Loan amount: #Form.Principal#<br>
Annual percentage rate: #Form.AnnualPercent#<br>
Loan duration: #Form.Months# months<br>
TOTAL INTEREST: #myInterest#<br>
</cfoutput>
<cfcatch type="InvalidData">
<cfoutput>
#cfcatch.message#<br>
</cfoutput>
</cfcatch>