Calling User-Defined Functions

Regardless of whether you are dealing with a tag- or script-based UDF, you call it the same way you do any other CFML function—within a cfoutput block, within a cfset tag, in any tag attribute, and within CFScript code:

  #MyFunction("test", 12)#
<cfset x = MyFunction( )>
<cfmytag value="#MyFunction('test')#">
  x = MyFunction(MyOtherFunction( ));

The simplest way to call a UDF is to define it, then call it in the same template. A function defined in the template in which it is called may be defined anywhere on the page, including after it has already been referenced (ColdFusion parses the entire page before processing the instructions). If you use this method, you should define all functions at the top of the page for readability and consistency. Using one of the Mean( ) functions we created, you can define it inline and call it like this:

<!--- // Mean(values [, delimiter])
      // Returns the mean (average) value of a list of delimited values.
<cffunction name="Mean" returntype="Numeric" output="No">
  <cfargument name="Values" type="String" required="Yes">
  <cfargument name="Delimiter" type="String" required="No" default=",">
  <cfreturn ArrayAvg(ListToArray(Arguments.Values, Arguments.Delimiter))>
<cfset MyValues="1|2|3|4|5|6|7|8|9|10">
The mean is: #Mean(MyValues, "|")#

Notice that when we call the Mean( ) function, we call it with two arguments. This is known as positional ...

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.