User Guide

Table Of Contents
Creating user-defined functions 175
You can embed CFScript code inside the function definition.
The cffunction tag provides attributes that enable you to easily limit the execution of the tag
to authorized users or specify how the function can be accessed.
The following code uses the
cffunction tag to define the exponentiation function:
<cffunction name="twoPower" output=True>
<cfargument name="exponent">
<cfreturn 2^exponent>
</cffunction>
For more information on how to use the cffunction tag to define a function, see “Defining
functions using the cffunction tag” on page 175.
Defining functions using the cffunction tag
The
cffunction and cfargument tags let you define functions in CFML without using
CFScript.
Note: This chapter describes how to use the cffunction tag to define a function that is not part of a
ColdFusion component. For information on ColdFusion components, see Chapter 10, “Building and
Using ColdFusion Components,” on page 201. For more information on the cffunction tag, see
CFML Reference.
The cffunction tag function definition format
A
cffunction tag function definition has the following format:
<cffunction name="functionName" [returnType="type" roles="roleList"
access="accessType" output="Boolean"]>
<cfargument name="argumentName" [Type="type" required="Boolean"
default="defaultValue">]
<!--- Function body code goes here. --->
<cfreturn expression>
</cffunction>
where square brackets ([]) indicate optional arguments. You can have any number of cfargument
tags.
The
cffunction tag specifies the name you use when you call the function. You can optionally
specify other function characteristics, as described in the following table:
Attribute Description
name The function name.
returnType (Optional) The type of data that the function returns. The valid standard type names
are: any, array, binary, boolean, date, guid, numeric, query, string, struct, uuid,
variableName, xml, and void. If you specify any other name, ColdFusion requires the
argument to be a ColdFusion component with that name.
ColdFusion throws an error if you specify this attribute and the function tries to return
data with a type that ColdFusion cannot automatically convert to the one you specified.
For example, if the function returns the result of a numeric calculation, a returnType
attribute of string or numeric is valid, but array is not.