User Guide

Table Of Contents
198 Chapter 9: Writing and Calling User-Defined Functions
Identifying and checking for UDFs
You can use the
IsCustomFunction function to determine whether a name represents a UDF.
The
IsCustomFunction function generates an error if its argument does not exist. As a result,
you must ensure that the name exists before calling the function, for example, by calling the
IsDefined function. The following code shows this use:
<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 IsCustomFunction in quotation marks, so you can use this
function to determine if function arguments are themselves functions.
Using the Evaluate function
If your user-defined function uses the
Evaluate function on arguments that contain strings, you
must make sure that all 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 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.