User Guide

Table Of Contents
172 Chapter 9: Writing and Calling User-Defined Functions
Mathematical calculation routines, including standard trigonometric and statistical operations
or calculating loan amortization
Routines that call functions externally, for example using COM or CORBA, including
routines to determine the space available on a Windows file system drive
For information about selecting among User-defined functions, ColdFusion components, and
custom tags, see Chapter 8, “Creating ColdFusion Elements,” on page 161.
Note: The Common Function Library Project at www.cflib.org is an open source collection of CFML
user-defined functions.
To use a user-defined function, you define the function and then call it.
Creating user-defined functions
Before you create a UDF, you must determine where you want to define it, and whether you want
to use CFML or CFScript to create it.
Determining where to create a user-defined function
You can define a function in the following places:
In a ColdFusion component. If you organize your functions in ColdFusion components, you
use the functions as described in “Using ColdFusion components” on page 216.
On the page where it is called. You can even define it below the place on the page where it is
called, but this poor coding practice can result in confusing code.
On a page that you include using a cfinclude tag. The cfinclude tag must be executed
before the function gets called. For example, you can define all your applications functions on
a single page and place a
cfinclude tag at the top of pages that use the functions.
On any page that puts the function name in a scope common with the page on which you call
the function. For more information on UDF scoping, see “Specifying the scope of a function
on page 194.
On the Application.cfc or Application.cfm page. For more information, see Chapter 13,
“Designing and Optimizing a ColdFusion Application,” on page 275
For recommendations on selecting where you define functions, see the sections “Using
Application.cfm and function include files” on page 194 and “Specifying the scope of a function
on page 194.
About creating functions using CFScript
You use the
function statement to define the function in CFScript. CFScript function
definitions have the following features and limitations:
The function definition syntax is familiar to anyone who uses JavaScript or most programming
languages.
CFScript is efficient for writing business logic, such as expressions and conditional operations.
CFScript function definitions cannot include CFML tags.