April 18, 2021
ColdFusion 101: Tags, Script and Functions, Part 3 – Functions
April 18, 2021
ColdFusion 101: Tags, Script and Functions, Part 3 – Functions
Wizard 47 posts
Followers: 24 people


I love ColdFusion.  I’ve been developing websites, applications, and software using ColdFusion for over 20 years.  I’ve struggled with the reputation ColdFusion has, and I’ve watched the customer and developer base dwindle.  I don’t believe that ColdFusion is a dead or dying language, but I believe that bringing new developers into the fold starts with the basics.  We all started programming somewhere, and I believe there is a dearth of “basic training” available for ColdFusion newcomers.  This series is intended to help with that and provide a resource to developers who are just getting started with ColdFusion.


Articles written in this series will assume a few things:

  • You have little to no knowledge or experience building web based applications.
  • You are using a modern version of ColdFusion.  As of the time of this writing, I will assume that new developers and new development will be utilizing ColdFusion 2021 or ColdFusion 2018.
  • As such, please understand that there may be some limitations in older versions.  Consult documentation for the version of ColdFusion you are coding against for compatibility.

ColdFusion Functions – The final building block of CFML

ColdFusion functions act upon data that you have in your code.  Think of a function as something that takes information you give it, processes it, and provides a modified version of that data.  A full list of ColdFusion functions can be found at https://cfdocs.org/functions.

Functions have a similar syntax.  They all begin with the name of the function, followed by parenthesis which contain any attributes you may want to pass to the function.  Much like CFML tags, some functions have required attributes, some have optional attributes, and some have no attributes at all.

Let’s take a look at some basic ColdFusion functions in action.  Consider the following code example:


In ColdFusion, a pound sign (or hashtag) indicates code that needs to be parsed by the server and it’s output translated before being sent back to the browser.  In the code sample above, there is a function that is written between the two pound signs.  The now() function gets the current date and time of the computer running the CFML server, so the output of this code will look like this:

{ts '2021-05-01 04:29:04'}

This isn’t very readable, so let’s apply a function to that function in order to change the result.  Consider the following code example:


Executing this code will result in the following output:

May 1, 2021 4:29:04 AM UTC

This is a basic example of how functions are used to manipulate and change data within your application.

Function Attributes

As I stated, some functions have no attributes, such as the now() function.  Other functions have required attributes.  The dateFormat() function

There are different ways you can pass attributes into a function.  Each function has a default order in which attributes are passed.  You can also name the attributes passed to a function and pass them out of the default order.  By default, the dateFormat() function takes two attributes in the following order; date and mask.  Date is the date to be formatted.  Mask is the formatting to apply to the date.

Consider the following code examples:

<div>#dateFormat( now(), "long" )#</div>
<div>#dateFormat( mask = "long", date = now() )#</div>

Both of these lines of code result in the same output, but by using named parameters, I was able to pass the values out of the default order.  There’s no advantage to passing parameters out of order (and quite frankly, I use named parameters most of the time, and still pass them in the default order) but where it comes in handy is when a function includes optional parameters.

For example, say you wanted to use ColdFusion’s hash() function to create a one-way hash on a string.  The hash() function takes one required parameter and three optional parameters:

  • String – the string being hashed.
  • Algorithm – the algorithm used to hash the data, defaulting to "MD5".
  • Encoding – the encoding to use in the resulting hash, defaulting to "UTF-8".
  • additionalIterations – a number representing the number of times the string should be hashed in order to produce the result, defaulting to 0.

Let’s assume you’re fine with accepting the default algorithm and the default encoding, but you want to iterate over the hash 10 times before producing the result.

Consider the following code example which does not use named parameters:

#hash( "My String", "MD5", "UTF-8", 10)#

Versus the the following code example which uses named parameters:

#hash( string = "My String", additionalIterations = 10)#

By using named parameters, I’ve been able to omit optional parameters that are left to their default values.

These are many different types of ColdFusion functions.  A list of all of the CFML functions can be found at https://cfdocs.org/functions.

More ways to learn ColdFusion

For more information on learning ColdFusion, https://www.learncfinaweek.com is an excellent information source that covers many of the basic concepts of ColdFusion development.

http://trycf.com and http://cffiddle.org are both websites that allow you to execute ColdFusion code without having to run an instance of ColdFusion.

CommandBox from Ortus Solutions is an excellent, free way to spin up and execute ColdFusion code locally.

Happy coding!

Add Comment