208
Chapter 10: Writing and Calling User-Defined Functions
Using the Request scope for static variables and constants
This section describes how to partially break the rule described in the section
“Referencing caller
variables” on page 203
. Here, the function defines variables in the Request scope. However, it is a
specific solution to a specific issue, where the following circumstances exist:
•
Your function initializes a large number of variables.
•
The variables have either of the following characteristics:
■
They must be
static
: they are used only in the function, the function can change their values,
and their values must persist from one invocation of the function to the next.
■
They are
named constants
; that is the variable value never changes.
•
Your application page (and any custom tags) calls the function multiple times.
•
You can assure that the variable names are used only by the function.
In these circumstances, you can improve efficiency and save processing time by defining your
function’s variables in the Request scope, rather than the Function scope. The function tests for
the Request scope variables and initializes them if they do not exist. In subsequent calls, the
variables exist and the function does not reset them.
The
NumberAsString
function, written by Ben Forta and available from www.cflib.org, takes
advantage of this technique.
Using function names as function arguments
Because function names are ColdFusion variables, you can pass a function’s name as an argument
to another function. This technique allows a function to use another function as a component.
For example, a calling page can call a calculation function, and pass it the name of a function that
does some subroutine of the overall function.
This way, the calling page could use a single function for different specific calculations, such as
calculating different forms of interest. The initial function provides the framework, while the
function whose name is passed to it can implement a specific algorithm that is required by the
calling page.
The following simple example shows this use. The
binop
function is a generalized function that
takes the name of a function that performs a specific binary operation and two operands. The
binop
function simply calls the specified function and passes it the operands. This code defines a
single operation function, the sum function. A more complete implementation would define
multiple binary operations.
<cfscript>
function binop(operation, operand1, operand2)
{ return (operation(operand1, operand2); }
function sum(addend1, addend2)
{ return a addend2;}
x = binop(sum, 3, 5);
writeoutput(x);
</cfscript>
Handling query results using UDFs
When you call a UDF in the body of a tag that has a
query
attribute, such as a
cfloop
query=...
tag, any function argument that is a query column name passes a single element of the
column, not the entire column. Therefore, the function must manipulate a single query element.
Summary of Contents for COLDFUSION MX 61-DEVELOPING 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: ......