UDF Basics

To create a tag-based UDF, you use the cffunction tag. Optionally, one or more cfargument tags can be used to define both required and optional arguments the function should expect as input. Data can be returned from the UDF using the cfreturn tag. Here’s some code that shows the basic syntax for a tag-based UDF:

<cffunction name="function_name" 
            output="Yes|No" 
            returntype="return_type"
            roles="authorized_roles">
  <cfargument name="arg1" type="arg_type" required="Yes|No" 
              default="default_val">
  <cfargument name="arg2" type="arg_type" required="Yes|No"
              default="default_val">
  ...
  additional CFML 
  <cfreturn expression>
</cffunction>

The cffunction tag pair acts as a container for the UDF. The name attribute is required; it specifies the name for the function. There are several guidelines you should think about when deciding what to name your function, to avoid both errors and organizational problems:

  1. Function names must begin with a letter, underscore, or Unicode currency symbol ($) and can contain only letters, numbers, underscores, and Unicode currency symbols.

  2. UDF names cannot contain periods.

  3. UDFs can’t have the same name as existing CFML functions.

  4. You can’t have more than one UDF with a given name available on the same template.

  5. When deciding on a name for your UDF, consider the naming conventions used by ColdFusion’s built-in functions. For example, list functions all begin with the word “list”: ListLen( ), ListLast( ), etc.

The next attribute in the cffunction tag ...

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.