Adobe COLDFUSION 9 Manual page 181

Developing applications
Hide thumbs Also See for COLDFUSION 9:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Building Blocks of ColdFusion Applications
Identifying and checking for UDFs
You can use the
IsCustomFunction
function generates an error if its argument does not exist. As a result, ensure that the name exists
IsCustomFunction
before calling the function, for example, by calling the
<cfscript>
if(IsDefined("MyFunc"))
if(IsCustomFunction(MyFunc))
WriteOutput("MyFunc is a user-defined function");
else
WriteOutput("Myfunc is defined but is NOT a user-defined function");
else
WriteOutput("MyFunc is not defined");
</cfscript>
You do not surround the argument to
determine if function arguments are themselves functions.
Using the Evaluate function
If your user-defined function uses the
variable names you use as arguments include the scope identifier. Doing so avoids conflicts with function-only
variables.
The following example returns the result of evaluating its argument. It produces the expected results, the value of the
argument, if you pass the argument using its fully scoped name, Variables.myname. However, the function returns the
value of the function local variable if you pass the argument as myname, without the Variables scope identifier.
<cfscript>
myname = "globalName";
function readname(name) {
var myname = "localName";
return (Evaluate(name));
}
</cfscript>
<cfoutput>
<!--- This one collides with local variable name. --->
The result of calling readname with myname is:
#readname("myname")# <br>
<!--- This one finds the name passed in. --->
The result of calling readname with Variables.myname is:
#readname("Variables.myname")#
</cfoutput>
Using recursion
A recursive function is a function that calls itself. Recursive functions are useful when an algorithm that repeats the
same operation multiple times using the results of the preceding repetition can solve the problem. 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 continues 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 to determine whether a name represents a UDF. The
IsDefined
in quotation marks, so you can use this function to
IsCustomFunction
function on arguments that contain strings, Make sure that all
Evaluate
Last updated 8/5/2010
function. The following code shows this use:
176

Advertisement

Table of Contents
loading

Table of Contents