Using Recursion; Handling Errors In Udfs - MACROMEDIA COLDFUSION MX 61-DEVELOPING COLDFUSION MX Develop Manual

Developing coldfusion mx applications
Table of Contents

Advertisement

</br>
<cfdump var="#arrayStruct#">
You must use the same structure element name for the array (in this case Array) in the calling page
and the function.

Using recursion

A recursive function is a function that calls itself. Recursive functions are useful when a problem
can be solved by an algorithm that repeats the same operation multiple times using the results of
the preceding repetition. Factorial calculation, used in the following example, is one case where
recursion is useful. The Towers of Hanoi game is also solved using a recursive algorithm.
A recursive function, like looping code, must have an end condition that always stops the
function. Otherwise, the function will continue until a system error occurs or you stop the
ColdFusion server.
The following example calculates the factorial of a number, that is, the product of all the integers
from 1 through the number; for example, 4 factorial is 4 X 3 X 2 X 1 = 24.
function Factorial(factor) {
If (factor LTE 1)
return 1;
else
return factor * Factorial(factor -1);
}
If the function is called with a number greater than 1, it calls itself using an argument one less
than it received. It multiplies that result by the original argument, and returns the result.
Therefore, the function keeps calling itself until the factor is reduced to 1. The final recursive call
returns 1, and the preceding call returns 2 * 1, and so on until all the initial call returns the end
result.
Caution: If a recursive function calls itself too many times, it causes a stack overflow. Always test any
recursive functions under conditions that are likely to cause the maximum number of recursions to
ensure that they do not cause a stack overflow.

Handling errors in UDFs

This section discusses the following topics:
Displaying error messages directly in the function
Returning function status information to the calling page
Using
/
try
catch
and generate exceptions
The technique you use depends on the circumstances of your function and application and on
your preferred programming style. However, most functions should use the second or third
technique, or a combination of the two. The following sections discuss the uses, advantages, and
disadvantages of each technique, and provides examples of their use.
Displaying error messages
Your function can test for errors and use the
directly to the user. This method is particularly useful for providing immediate feedback to users
for simple input errors. You can use it independently or in conjunction with either of the other
two error-handling methods.
212
Chapter 10: Writing and Calling User-Defined Functions
or
/
blocks and the
cftry
cfcatch
and
cfthrow
function to display an error message
WriteOutput
tags to handle
cfrethrow

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion mx

Table of Contents