Showcase and discover digital art at yex

Follow Design Stacks

Subscribe to our free newsletter to get all our latest tutorials and articles delivered directly to your inbox!

ColdFusion MX Overview – UDFs Enhanced

Page 4 — UDFs Enhanced

Three of the most important new tags in CFML are the new <CFFUNCTION>, <CFARGUMENT> and <CFRETURN> tags for writing User Defined Functions, or UDFs. When UDFs were first introduced in ColdFusion 5.0, developers were required to use the <CFSCRIPT> syntax to define each UDF. Two problems with <CFSCRIPT> are that ColdFusion tags can not appear inside a <CFSCRIPT> block, and <CFSCRIPT> itself is an unfamiliar syntax to newbies. With <CFFUNCTION>, you can finally mix tags into your function definitions. Lets take a look at a simple function written in both the <CFSCRIPT> syntax versus the new tag format.

Old <CFSCRIPT> Style

 function getTomorrow () {
   return dayofweekAsString(dayofweek(dateadd(“d”, 1, now())));

<CFOUTPUT>Tomorrow is #getTomorrow()#.</CFOUTPUT>


<CFFUNCTION NAME=”getTomorrow”>
 <CFRETURN dayofweekAsString(dayofweek(dateadd(“d”, 1, now())))>

<CFOUTPUT>Tomorrow is #getTomorrow()#.</CFOUTPUT>

Both functions above work exactly the same way even though they are written in different formats. As you can see, the <CFFUNCTION> tag is a paired tag, meaning it has a start and end tag. Everything that appears inside is the definition of the User Defined Function. This example doesn’t have very much logic, and doesn’t accept any parameters. Let’s take a look at another UDF written with <CFFUNCTION> that uses the <CFARGUMENT> tag.

<CFFUNCTION name=”innerTrim”>
 <CFARGUMENT name=”incomingString”>
 <CFSET stringWithoutSpaces = replace(incomingString, ” “, “”, “ALL”)>
  ACTION = “write”
  FILE   = “C:sample.txt”
  OUTPUT = “#stringWithoutSpaces#”>
 <CFRETURN stringWithoutSpaces>

This simple UDF will accept a variable, remove all of the spaces, write the string to a file, then return the result. For example, lets pretend there is a social security variable called VARIABLES.SSN which contains the text “123 456 7890”. We could run the following line to eliminate the spaces, write the content to C:sample.txt, and also put the resulting string into #VARIABLES.SSN#.


In this example, we only returned a string, however functions can also return numbers, record sets, structures, arrays, dates, bits, binary data and references to components.

Hopefully you can see how writing UDFs with <CFFUNCTION> can make your life simpler than doing them with <CFSCRIPT>. Check out for a nice collection of open source UDFs written in both <CFSCRIPT> and <CFFUNCTION>.

You may recognize that a <CFFUNCTION> can solve problems that a custom tag can solve too. A custom tag is easy to use because you can use it anywhere in your code without paying attention to what comes before it. With UDFs, the definition must appear before the point at which you call it from, or you must <CFINCLUDE> the function definition before using it.

As a general rule, if you need to call code that performs a special task but doesn’t necessarily have a result you need to work with, write a custom tag. If you need to write a routine that will help you set variables or evaluate to a single result, write a UDF.

One other important use of <CFFUNCTION> tags that I haven’t mentioned yet is that you can create ColdFusion components with them. Now that you are a whiz with the <CFFUNCTION> tag and you understand its place in the world of ColdFusion, we’ll build on all we have learned thus far by an introduction to ColdFusion components, or CFCs.