User Guide

Table Of Contents
Creating user-defined functions 177
Using a CFML tag in a user-defined function
The most important advantage of using the
cffunction tag over defining a function in CFScript
is that you can include CFML tags in the function. Thus, UDFs can encapsulate activities, such
as database lookups, that require ColdFusion tags. Also, you can use the
cfoutput tag to display
output on the calling page with minimal coding.
Tip: To improve performance, avoid using the cfparam tag in ColdFusion functions. Instead, use the
cfset tag.
The following example function looks up and returns an employees department ID. It takes one
argument, the employee ID, and looks up the corresponding department ID in the
cfdocexamples Employee table:
<cffunction name="getDeptID" >
<cfargument name="empID" required="true" type="numeric">
<cfset var cfdocexamples="">
<cfquery dataSource="cfdocexamples" name="deptID">
SELECT Dept_ID
FROM Employee
WHERE Emp_ID = #empID#
</cfquery>
<cfreturn deptID.Dept_ID>
</cffunction>
Rules for function definitions
The following rules apply to functions that you define using CFScript or the
cffunction tag:
The function name must be unique. It must be different from any existing variable, UDF, or
built-in function name, except you can use the ColdFusion advanced security function names.
The function name must not start with the letters cf in any form. (For example,
CF_MyFunction, cfmyFunction, and cfxMyFunction are not valid UDF names.)
You cannot redefine or overload a function. If a function definition is active, ColdFusion
generates an error if you define a second function with the same name.
You cannot nest function definitions; that is, you cannot define one function inside another
function definition.
The function can be recursive, that is, the function definition body can call the function.
The function does not have to return a value.
You can use tags or CFScript to create a UDF. Each technique has advantages and disadvantages.