Using the Request scope for static variables and constants
This section describes how to partially break the rule described in the section
variables" on page
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
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
takes the name of a function that performs a specific binary operation and two operands. The
function simply calls the specified function and passes it the operands. This code defines a
binop
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 addend1 + 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
tag, any function argument that is a query column name passes a single element of the
query=...
column, not the entire column. Therefore, the function must manipulate a single query element.
208
Chapter 10: Writing and Calling User-Defined Functions
203. Here, the function defines variables in the Request scope. However, it is a
function, written by Ben Forta and available from www.cflib.org, takes
function is a generalized function that
binop
attribute, such as a
query
"Referencing caller
cfloop
Need help?
Do you have a question about the COLDFUSION MX 61-DEVELOPING COLDFUSION MX and is the answer not in the manual?
Questions and answers