User-Defined Functions

In ColdFusion 5.0, you now have the ability to create user-defined functions (UDFs) that can be called inline, just like other ColdFusion functions. The premise behind UDFs is to allow you to extend ColdFusion’s core set of functions with custom ones that encapsulate programming tasks not currently addressed by the language. 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 as often as you want in your template.

Before we get into the specifics of defining and calling UDFs, you should be aware of a few rules related to their use:

  • UDFs can be used anywhere you use a normal CFML function: within a CFOUTPUT block, within CFSET tags, in any tag attributes, and within CFScript code.

  • UDFs can be called recursively, which means that a UDF can call itself.

  • UDFs can call other UDFs.

  • Because UDFs are written using CFScript, they can’t contain any CFML tags.

  • Although it’s possible to assign a UDF to a variable, it’s generally not advisable to do so. This is especially true in the case of persistent variables (session, application, server) due to the locking issues this presents.

Defining Custom Functions

All UDFs are defined within a CFSCRIPT block and follow the same basic format. You can define as many functions as you wish ...

Get Programming ColdFusion 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.