Functions

Typically, a function acts on data. It can generate a value or a set of values, usually from some input. You can perform the following operations (actions) with functions:

Functions and number signs

You use number signs (#) with functions to display the results of a function on the page. Number signs tell the ColdFusion server to evaluate the content between the number signs and display the value, for example:

<cfoutput> 
	Hello world, <br>
	Today’s date is #DateFormat(Now(), "mm/dd/yyyy")# 
</cfoutput>

The following figure shows the output of this example:


This image shows that the results of the output are:Hello world,Today’s date is 02/27/2002

If you did not include the number signs around the DateFormat(Now(), "mm/dd/yyyy") function, ColdFusion would not evaluate the function and the previous example would display your source code, as follows:


This image shows the results if pound signs were not included around function:Hello world,Today’s date is DateFormate(Now), "mm/dd/yy")

For more information about how to use number signs with functions, see ColdFusion MX Developer’s Guide.

Functions and parentheses

All functions have parentheses, regardless of whether the function acts on data. Consider the following function:

#Now()#

If you put anything inside the parentheses of the Now() function, an error would occur. The Now() function returns an unformatted date and time. However, you can format the results of this function with other functions, such as the DateFormat() or TimeFormat() functions.

Using functions on values

Usually, a function performs an operation on a value, and the value can include the value of a variable. For example, to format the value of a variable that contains a value in dollars, the code to write this statement might look like this:

#DollarFormat(price)#

The DollarFormat function returns a value as a string and formats that value with two decimal places, a thousands separator, and a dollar sign. The number signs (#) around the function instruct ColdFusion to evaluate the content between the number signs and display the value.

Nesting functions

Functions can generate data, as well as act on data. Consider the following example:

#DateFormat(Now(), "mm/dd/yyyy")#

In this example, the Now() function generates the date, and then the DateFormat function formats the date.