250
Chapter 13 Extending ColdFusion Pages with CFML Scripting
Defining and Using Custom Functions
You can define
custom
functions (also known as
user-defined functions
) and use
them in your application pages as you do standard ColdFusion functions. This
allows you to create a function for an algorithm or procedure that you use frequently,
and then use the function wherever you need the procedure. If you must change the
procedure, you change only one piece of code. You can use your function anywhere
that you can use a ColdFusion expression: in tag attributes, between # signs in
output, and in CFScript code.
Defining functions
You define functions using CFScript, in a manner similar to defining JavaScript
functions. The function must return a value. Functions can be recursive, that is, the
body of a function can call the function.
You can define a function in the following places:
•
On the page where it is called (even after it is called, although this is not
recommended).
•
On a page that you include using a
cfinclude
tag. The
cfinclude
tag must be
executed before the function gets called. For example, you can define all your
application’s functions on a single page and place a
cfinclude
tag at the top of
pages that use the functions.
Syntax
Use the following syntax inside a
cfscript
tag to define a function:
function
functionName
(
[paramName1[, paramName2...]]
)
{
CFScript Statements
}
functionName
The name of the function. You cannot use the name of an standard ColdFusion
function name. You cannot use the same name for two different function
definitions. Function names cannot include periods.
paramName1
...
Names of the parameters required by the function. The number of arguments
passed into the function must equal or exceed the number of parameters in the
function definition. If the calling page omits any of the required parameters,
ColdFusion generates a mismatched argument count error.
The body of the function definition must consist of one or more valid CFScript
statements.
Summary of Contents for COLDFUSION 5-DEVELOPING
Page 1: ...Macromedia Incorporated Developing ColdFusion Applications MacroMedia ColdFusion 5 ...
Page 58: ...38 Chapter 3 Querying a Database ...
Page 134: ...114 Chapter 7 Updating Your Database ...
Page 210: ...190 Chapter 10 Reusing Code ...
Page 232: ...212 Chapter 11 Preventing and Handling Errors ...
Page 238: ...218 Chapter 12 Using the Application Framework ...
Page 262: ...242 Chapter 12 Using the Application Framework ...
Page 278: ...258 Chapter 13 Extending ColdFusion Pages with CFML Scripting ...
Page 320: ...300 Chapter 15 Indexing and Searching Data ...
Page 336: ...316 Chapter 16 Sending and Receiving E mail ...
Page 374: ...354 Chapter 18 Interacting with Remote Servers ...