MACROMEDIA COLDFUSION MX 61-DEVELOPING COLDFUSION MX Develop Manual page 197

Developing coldfusion mx applications
Table of Contents

Advertisement

Using the Arguments scope as a structure
The following rule applies when referencing Arguments scope as a structure:
Use the argument names as structure keys. For example, if your function definition includes a
Principal argument, refer to the argument as Arguments.Principal.
The following rules are also true, but avoid writing code that uses them. To ensure program clarity,
only use the Arguments structure for arguments that you name in the function definition. Use the
Arguments scope as an array for optional arguments that you do not declare in the function
definition.
If the function can take unnamed optional arguments, use array notation to reference the
unnamed arguments. For example, if the function declaration includes two named arguments
and you call the function with three arguments, refer to the third argument as Arguments[3].
To determine if an unnamed optional argument exists, use the
example,
structKeyExists(Arguments,"3")
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 refer to 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.
Function-only variables
In addition to the Arguments scope, each function can have a number of 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.
In CFScript, you create function-only variables with the
you never prefix function-only variables with a scope name.
For more information on using function-only variables, see
on page
203.
Good argument naming practice
An argument's name should represent its use. For example, the following code is unlikely to result
in confusion:
<cfscript>
function SumN(Addend1,Addend2)
{ return Addend1 + Addend2; }
</cfscript>
<cfset x = 10>
<cfset y = 12>
<cfoutput>#SumN(x,y)#<cfoutput>
The following, similar code is more likely to result in programming errors:
<cfscript>
function SumN(x,y)
{ return x + y; }
</cfscript>
<cfset x = 10>
<cfset y = 12>
<cfoutput>#SumN(x,y)#<cfoutput>
StructKeyExists
.
statement. Unlike other variables,
var
"Using function-only variables"
Creating user-defined functions
function; for
197

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion mx

Table of Contents