192
Chapter 10: Writing and Calling User-Defined Functions
About user-defined functions
You can create
user-defined functions
, or UDFs (also known as custom functions), and use them in
your application pages as you do standard ColdFusion functions. This lets you 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 pound (#) signs in output, and in CFScript code. Typical uses of UDFs include, but are
not limited to the following:
•
Data manipulation routines, such as a function to reverse an array
•
String and date/time routines, such as a function to determine whether a string is a valid IP
address
•
Mathematical calculation routines, including standard trigonometric and statistical operations
or calculating loan amortization
•
Routines that call functions externally, for example using COM or CORBA, including
routines to determine the space available on a Windows file system drive
For information about selecting among User-defined functions, custom tags, and ColdFusion
components, see
Chapter 8, “Reusing Code in ColdFusion Pages,” on page 163
.
Note:
The Common Function Library Project at http://www.cflib.org is an open source collection of
CFML user-defined functions.
To use a user-defined function, you define the function and then call it. Typically you define the
function on your ColdFusion page or a page that you include. You can also define the function on
one page and put it in a scope that is shared with the page that calls it. (For more information on
UDF scoping, see
“Specifying the scope of a function” on page 206
.) You can also put commonly
used functions on a single ColdFusion page and include it in your Application.cfm page.
Calling user-defined functions
You can call a UDF in two ways:
•
With unnamed, positional arguments, as you would call a built-in function
•
With named arguments, as you would use attributes in a tag
You can use either technique for any function. However, if you use named arguments, you must
use the same argument names to call the function as you use to define the function. You cannot
call a function with a mixture of named and unnamed arguments. For more information on
calling functions with and without argument names, see
“Calling functions and using variables”
on page 202
.
One example of a user-defined function is a TotalInterest function that calculates loan payments
based on a principal amount, annual percentage, and loan duration in months (For this function’s
definition, see
“A User-defined function example” on page 204
). You might call the function
without argument names on a form’s action page, as follows:
<cfoutput>
Interest: #TotalInterest(Form.Principal, Form.Percent, Form.Months)#
</cfoutput>
Summary of Contents for ColdFusion MX
Page 1: ...Developing ColdFusion MX Applications...
Page 22: ...22 Contents...
Page 38: ......
Page 52: ...52 Chapter 2 Elements of CFML...
Page 162: ......
Page 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Page 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Page 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Page 266: ......
Page 314: ...314 Chapter 14 Handling Errors...
Page 344: ...344 Chapter 15 Using Persistent Data and Locking...
Page 349: ...About user security 349...
Page 357: ...Security scenarios 357...
Page 370: ...370 Chapter 16 Securing Applications...
Page 388: ...388 Chapter 17 Developing Globalized Applications...
Page 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Page 410: ......
Page 426: ...426 Chapter 19 Introduction to Databases and SQL...
Page 476: ...476 Chapter 22 Using Query of Queries...
Page 534: ...534 Chapter 24 Building a Search Interface...
Page 556: ...556 Chapter 25 Using Verity Search Expressions...
Page 558: ......
Page 582: ...582 Chapter 26 Retrieving and Formatting Data...
Page 668: ......
Page 734: ...734 Chapter 32 Using Web Services...
Page 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Page 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Page 788: ......