Chapter 20. User-Defined Functions

Since their introduction in ColdFusion 5.0, user-defined functions (UDFs) have become a cornerstone of the CFML language. UDFs give you the ability to create custom that can be called inline, just like other ColdFusion functions. UDFs allow you to extend ColdFusion’s core set of functions with custom functions that encapsulate programming tasks not currently addressed by CFML. For example, there is no CFML function for calculating the mean (average value) of a delimited list of numeric values. Rather than writing code that uses multiple functions each time you want to calculate the mean for a given list of numbers, you can create a UDF that can be called inline in your template as often as you want.

This chapter covers everything you need to know to create your own UDFs. Included in the discussion are good practices for developing, organizing, and deploying user-defined functions. We’ll also talk about advanced topics such as error- and exception-handling strategies.

In ColdFusion MX, you have two options for creating user-defined functions. You can write UDFs using the new cffunction, cfargument, and cfreturn tags, or you can use the older and less feature-rich CFScript-based syntax.[1] There are pros and cons to both options, which you should carefully consider when deciding which method to use. The major differences are shown in Table 20-1.

Table 20-1. Differences between tag- and script-based UDFs

Tag-based UDFs

Script-based UDFs

Get Programming ColdFusion MX, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.