User Guide

Table Of Contents
32 Chapter 2: Elements of CFML
User-defined functions
You can write your own functions, user-defined functions (UDFs). You can use these functions in
ColdFusion expressions or in CFScript. You can call a user-defined function anywhere you can
use a built-in CFML function. You create UDFs using the
cffunction tag or the CFScript
function statement. UDFs that you create using the cffunction tag can include ColdFusion
tags and functions. UDFs that you create in CFScript can only include functions. You can create
stand-alone UDFs or encapsulate them in a ColdFusion component.
User-defined functions let you encapsulate logic and operations that you use frequently in a single
unit. This way, you can write the code once and use it multiple times. UDFs ensure consistency of
coding and enable you to structure your CFML more efficiently.
Typical user-defined functions include mathematical routines, such as a function to calculate the
logarithm of a number; string manipulation routines, such as a function to convert a numeric
monetary value to a string such as “two dollars and three cents”; and can even include encryption
and decryption routines.
Note: The Common Function Library Project at www.cflib.org includes a number of free libraries of
user-defined functions.
For more information on user-defined functions, see Chapter 9, “Writing and Calling User-
Defined Functions,” on page 171.
ColdFusion components
ColdFusion components encapsulate multiple, related, functions. A ColdFusion component is
essentially a set of related user-defined functions and variables, with additional functionality to
provide and control access to the component contents. ColdFusion components can make their
data private, so that it is available to all functions (also called methods) in the component, but not
to any application that uses the component.
ColdFusion components have the following features:
They are designed to provide related services in a single unit.
They can provide web services and make them available over the Internet.
They can provide ColdFusion services that Flash clients can call directly.
They have several features that are familiar to object-oriented programmers, including data
hiding, inheritance, packages, and introspection.
For more information on ColdFusion components, see Chapter 10, “Building and Using
ColdFusion Components,” on page 201.
Constants
The value of a constant does not change during program execution. Constants are simple scalar
values that you can use within expressions and functions, such as “Robert Trent Jones” and
123.45. Constants can be integers, real numbers, time and date values, Boolean values, or text
strings. ColdFusion does not allow you to give names to constants.