O'Reilly logo

Programming ColdFusion MX, 2nd Edition by Rob Brooks-Bilson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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:

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

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))>
</cffunction>
   
<cfset MyValues="1|2|3|4|5|6|7|8|9|10">
   
<cfoutput>
The mean is: #Mean(MyValues, "|")#
</cfoutput>

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required