Calling functions and using variables
203
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 there is no
function scope variable named Customer_name) 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 refer to it anywhere in the
calling page
after
the function call. You cannot refer to the variable before you call the function.
However, you should generally avoid using the caller’s variables directly inside a function. Using
the caller’s variables creates a dependency on the caller. You must always ensure that the code
outside the function uses the same variable names as the function. This 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’s variables.
As with many programming practice, there are valid exceptions to this recommendation. For
example you might 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, as shown in
“Using the Request
scope for static variables and constants” on page 208
.).
•
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 function-only variables
Make sure to use the
var
statement in CFScript UDFs to declare all function-specific variables,
such as loop indexes and temporary variables that are required only for the duration of the
function call. Doing this 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
cfloop
tag with an index variable i, and the tag body
calls a CFScript UDF that 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
cfloop
tag body.
In general, use the
var
statement to declare all UDF variables, other than the function arguments
or shared-scope 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.
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: ......