User Guide

Table Of Contents
194 Chapter 9: Writing and Calling User-Defined Functions
Using UDFs effectively
This section provides information that will help you use user-defined functions more effectively.
Using functions in ColdFusion component
In many cases, the most effective use of UDFs is within a CFC. For more information on CFCs,
see Chapter 10, “Building and Using ColdFusion Components,” on page 201.
Using Application.cfm and function include files
Consider the following techniques for making your functions available to your ColdFusion pages:
If you consistently call a small number of UDFs, consider putting their definitions on the
Application.cfm page.
If you call UDFs in only a few of your application pages, do not include their definitions in
Application.cfm.
If you use many UDFs, put their definitions on one or more ColdFusion pages that contain
only UDFs. You can include the UDF definition page in any page that calls the UDFs.
The next section describes other techniques for making UDFs available to your ColdFusion
pages.
Specifying the scope of a function
User-defined function names are essentially ColdFusion variables. ColdFusion variables are names
for data. Function names are names (references) for segments of CFML code. Therefore, like
variables, functions belong to scopes.
About functions and scopes
Like ColdFusion variables, UDFs exist in a scope:
When you define a UDF, ColdFusion puts it in the Variables scope.
You can assign a UDF to a scope the same way you assign a variable to a scope, by assigning the
function to a name in the new scope. For example, the following line assigns the MyFunc UDF
to the Request scope:
<cfset Request.MyFunc = Variables.MyFunc>
You can now use the function from any page in the Request scope by calling Request.MyFunc.