Adobe COLDFUSION 9 Manual page 170

Developing applications
Hide thumbs Also See for COLDFUSION 9:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Building Blocks of ColdFusion Applications
In CFScript, you create function-only variables with the
function-only variables with a scope name.
Using function-only variables
Make sure to use the
statement in CFScript UDFs to declare all function-specific variables, such as loop indexes
var
and temporary variables that are required only for the duration of the function call. Doing so ensures that these
variables are available inside the function only, and makes sure that the variable names do not conflict with the names
of variables in other scopes. If the calling page has variables of the same name, the two variables are independent and
do not affect each other.
For example, if a ColdFusion page has a
also has a loop with a function-only index variable i, the UDF does not change the value of the calling page loop index,
and the calling page does not change the UDF index. So you can safely call the function inside the
In general, use the
statement to declare all UDF variables, other than the function arguments or shared-scope
var
variables, that you use only inside CFScript functions. Use another scope, however, if the value of the variable must
persist between function calls; for example, for a counter that the function increments each time it is called.
Referencing caller variables
A function can use and change any variable that is available in the calling page, including variables in the caller's
Variables (local) scope, as if the function was part of the calling page. For example, if you know that the calling page
has a local variable called Customer_name (and no function scope variable named Customer_name exists) the
function can read and change the variable by referring to it as Customer_name or (using better coding practice)
Variables.Customer_name. Similarly, you can create a local variable inside a function and then use it anywhere in the
calling page after the function call. You cannot use the variable before you call the function.
However, generally avoid using the caller's variables directly inside a function. Using the caller's variables creates a
dependency on the caller. Ensure that the code outside the function uses the same variable names as the function.
Doing so can become difficult if you call the function from many pages.
You can avoid these problems by using only the function arguments and the return value to pass data between the
caller and the function. Do not reference calling page variables directly in the function. As a result, you can use the
function anywhere in an application (or even in multiple applications), without concern for the calling code variables.
As with other programming practices, valid exceptions to this recommendation exist. For example, you can do any of
the following:
• Use a shared scope variable, such as an Application or Session scope counter variable.
• Use the Request scope to store variables used in the function. For more information, see
for static variables and
constants" on page 174.
• Create context-specific functions that work directly with caller data if you always synchronize variable names.
Note: If your function must directly change a simple variable in the caller (one that is not passed to the function by
reference), you can place the variable inside a structure argument.
Using arguments
Function arguments can have the same names, but different values, as variables in the caller. Avoid such uses for
clarity, however.
The following rules apply to argument persistence:
• Because ColdFusion passes simple variable and array arguments by value, their names and values exist only while
the function executes.
statement. Unlike other variables, you never prefix
var
tag with an index variable i, and the tag body calls a CFScript UDF that
cfloop
Last updated 8/5/2010
165
tag body.
cfloop
"Using the Request scope

Advertisement

Table of Contents
loading

Table of Contents