|
A function executes an action and returns the result as output. Functions help reuse the code, leading to modular programming. You create a function once and then call it in multiple instances in a program by using the function name.
Functions may need input data when they're referenced or called. This input data is called a parameter or an argument. Variables, expressions, and other functions can be passed as arguments. A function typically returns a single value, which can be a simple datatype, such as a number or a string, or a complex datatype, such as an array.
CFML provides several built-in functions. These functions are based on the datatypes that they work with, and they're broadly categorized as follows:
Mathematical functions
String functions
Date and time functions
Array functions
List functions
Formatting functions
Note | The Common Function Library at http://www.cflib.org/ includes a number of free libraries for user-defined functions. |
Mathematical functions work on numbers. Some of the most common mathematical functions are listed in Table 2.4.
Function | Syntax | Description |
---|---|---|
Abs | Abs (number) | Returns the absolute value of the number. |
BitAnd | BitAnd (number1,number2) | Returns the bitwise AND of two numbers. |
Max | Max(number1,number2) | Returns the higher value of the two numbers. |
String functions are used to manipulate string variables. For example:
<CFSET Phrase=LCase(Phrase)>
This function replaces the string value stored in Phrase with all the characters converted to lowercase. Some of the common string type functions are listed in Table 2.5.
Function | Syntax | Description |
---|---|---|
Insert | Insert(substring,string, position) | Inserts a substring in a string after a specified character position |
LTrim | LTrim(String) | Returns the string with leading spaces removed |
RJustify | RJustify(string,length) | Returns a right-justified string in the specified field length |
ColdFusion also has variables that store date and time values, which can range from 100 A.D to 9999 A.D. Date and time functions work with these variables.
For example, the CreateDateTime function is used to create a valid date/time object. The syntax of the CreateDateTime function is
CreateDateTime(year, month, day, hour, minute, second)
The year values 0 to 29 are interpreted as 21st-century dates. For example, to create a new date/time variable, use the <CFSET> tag as follows:
<CFSET DateToday = CreateDateTime(2002,11,25,14,15,50)>
The preceding code assigns the date 25th November, 2002 and time 14:15:50 to the variable DateToday. You can use the <CFOUTPUT> tag to display the value of a date/time variable. For example, the following code displays the value of the DateToday variable as 2002-11-25 14:15:50:
<CFOUTPUT>#DateToday#</CFOUTPUT>
Some of the common date and time functions are listed in Table 2.6.
Function | Syntax | Description |
---|---|---|
CreateDate | Create(year,month,date) | Returns a valid date object. |
DateFormat | DateFormat (date [,mask]) | Returns a formatted date/time value. mask is the set of characters that shows how ColdFusion displays a date. |
Now | Now() | Returns the current date and time. It's usually used as a parameter to other date and time functions. |
ColdFusion offers several functions to work with arrays. Some of the common array functions are described in Table 2.7.
Function | Syntax | Description |
---|---|---|
ArrayAppend | ArrayAppend (Array,Value) | Adds an element at the end of an array |
ArrayPrepend | ArrayAppend (Array,Value) | Adds an element at the beginning of an array |
ArrayDeleteAt | ArrayDeleteAt (Array,Position) | Deletes an element at a particular position |
ArrayInsertAt | ArrayInsertAt (Array,Position) | Inserts an element at a particular position |
Some of the common list functions are described in Table 2.8.
Function | Syntax | Description |
---|---|---|
ListLen | ListLen(List,Delimiters) | Returns the number of elements in a list |
ListFirst | ListFirst (List,Delimiters) | Returns the first element in a list |
ListAppend | ListAppend(List,Element,Delimiters) | Adds an element to a list |
ColdFusion provides functions to display output in a user-friendly format. Some of the formatting output functions are listed in Table 2.9.
Function | Syntax | Description |
---|---|---|
DollarFormat | DollarFormat(Number) | Returns a number as a string formatted with two decimal places, a thousands separator, and a dollar sign |
LSDateFormat | LSDateFormat (date [,mask]) | Formats the date portion using the locale convention |
|