Adobe COLDFUSION 9 Manual page 163

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

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Building Blocks of ColdFusion Applications
Using a CFML tag in a user-defined function
The most important advantage of using the
include CFML tags in the function. Thus, UDFs can encapsulate activities, such as database lookups, that require
ColdFusion tags. Also, you can use the
Note: To improve performance, avoid using the
The following example function looks up and returns an employee 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
• The function name must be unique. It must be different from any existing variable, or UDF, except that you can
use the ColdFusion advanced security function names.
• You can have a user-defined function with the same name as a built-in function for a CFC but not for CFM.
• You cannot use the following names to create user-defined functions:
• writedump
• writelog
• location
• throw
• trace
• 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.
Calling user-defined functions
You can call a function anywhere that you can use an expression, including in number signs (#) in a
a CFScript, or in a tag attribute value. One function can call another function, and you can use a function as an
argument to another function.
tag over defining a function in CFScript is that you can
cffunction
tag to display output on the calling page with minimal coding.
cfoutput
tag in ColdFusion functions. Instead, use the
cfparam
Last updated 8/5/2010
cfset
tag:
cffunction
cfoutput
158
tag.
tag, in

Advertisement

Table of Contents
loading

Table of Contents