Adobe 38043740 - ColdFusion Standard - Mac Development Manual page 169

Developing applications
Hide thumbs Also See for 38043740 - ColdFusion Standard - Mac:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Building Blocks of ColdFusion Applications
• If you do not name an optional argument in the function definition, but do use a name for it in the function call,
use the name specified in the function call For example, if you have an unnamed optional argument and call the
function using the name myOptArg for the argument, you can reference the argument as Arguments.myOptArg
in the function body. This usage, however, is poor programming practice, as it makes the function definition
contents depend on variable names in the code that calls the function.
Using the Arguments scope in CFScript
A function can have optional arguments that you do not have to specify when you call the function. To determine the
number of arguments passed to the function, use the following function:
ArrayLen(Arguments)
When you define a function using CFScript, the function must use the Arguments scope to retrieve the optional
arguments. For example, the following SumN function adds two or more numbers together. It requires two arguments
and supports any number of additional optional arguments. You can reference the first two, required, arguments as
and
or as
Arg1
Arg2
Arguments[1]
arguments as
Arguments[3]
function SumN(Arg1,Arg2) {
var arg_count = ArrayLen(Arguments);
var sum = 0;
var i = 0;
for( i = 1 ; i LTE arg_count; i = i + 1 )
{
sum = sum + Arguments[i];
}
return sum;
}
With this function, any of the following function calls are valid:
SumN(Value1, Value2)
SumN(Value1, Value2, Value3)
SumN(Value1, Value2, Value3, Value4)
and so on.
The code never uses the Arg1 and Arg2 argument variables directly, because their values are always the first two
elements in the Arguments array and it is simpler to step through the array. Specifying Arg1 and Arg2 in the function
definition ensures that ColdFusion generates an error if you pass the function one or no arguments.
Note: Avoid referring to a required argument in the body of a function by both the argument name and its place in the
Arguments scope array or structure, as doing so can be confusing and makes it easier to introduce errors.
Using the Arguments scope in cffunction definitions
When you define a function using the
arguments are named in the
"About the Arguments
scope" on page 161.
For more information on using the Arguments scope in functions defined using CFScript, see
scope in
CFScript" on page 164.
Function-only variables
In addition to the Arguments scope, each function can have variables that exist only inside the function, and are not
saved between times the function gets called. As soon as the function exits, all the variables in this scope are removed.
and
. Access the third, fourth, and any additional optional
Arguments[2]
,
, and so on
Arguments[4]
tag, you generally reference the arguments directly by name if all
cffunction
tags. If you do use the Arguments scope identifier, follow the rules listed in
cfargument
Last updated 1/20/2012
164
"Using the Arguments

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion 9

Table of Contents