Creating user-defined functions
197
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
StructKeyExists
function; for
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
var
statement. Unlike other variables,
you
never
prefix function-only variables with a scope name.
For more information on using function-only variables, see
“Using function-only variables”
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 A 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>
Summary of Contents for COLDFUSION MX 61-DEVELOPING 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: ......