User Guide

Table Of Contents
171
CHAPTER 9
Writing and Calling User-Defined Functions
This chapter describes how to create and call user-defined functions (UDFs).
Contents
About user-defined functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Creating user-defined functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Calling user-defined functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Working with arguments and variables in functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Handling errors in UDFs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
A user-defined function example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Using UDFs effectively . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
About user-defined functions
You can create your own custom functions, known as user-defined functions, or UDFs. You then
use them in your application pages the same way you use standard ColdFusion functions. You can
also organize functions you create by grouping related functions into ColdFusion components.
For more information, see Chapter 10, “Building and Using ColdFusion Components, on
page 201.
When you create a function for an algorithm or procedure that you use frequently, you can then
use the function wherever you need the procedure. If you must change the procedure, you change
only one piece of code. You can use your function anywhere that you can use a ColdFusion
expression: in tag attributes, between number (#) signs in output, and in CFScript code. Typical
uses of UDFs include, but are not limited to the following:
Data manipulation routines, such as a function to reverse an array
String and date/time routines, such as a function to determine whether a string is a valid
IP address