User Guide
Defining and Using Custom Functions 251
The following two statements are allowed only in function definitions. Each function
must have a return statement:
var variableName = initialValue;
Creates and initializes a variable that is local to the function (function variable).
This variable has meaning only inside the function and is not saved between
calls to the function. It has precedence in the function body over any variables
with the same name that exist in any other scopes. You never prefix a function
variable with a scope identifier, and their names cannot include periods.
All var statements must be at the top of the function declaration, before any
other statements. You must initialize all variables when you declare them. You
cannot use the same name for a function variable and a parameter.
return expression;
Evaluates expression, returns its value to the page that called the function, and
exits the function. You can return any valid ColdFusion variable type, including
structures, queries, and arrays.
Each function must execute a return statement.
Calling functions
You can call a function anywhere that you can use an expression, including in the
body of a
cfoutput tag, in a ColdFusion Script, or in a tag attribute value. One
function can call another function, and you can use a function as a parameter to
another function.
You call custom functions the same way you call any built-in ColdFusion functions.
Using arguments and variables
The following sections discuss optional arguments and their use, how arguments get
passed, including the effects that the passing methods have, and how you use
variables inside functions.
Optional arguments and the Arguments array
A function can have optional arguments that you do not specify as parameters in the
definition. For example, you can write the following function that requires two
arguments and supports three additional optional arguments:
function MyFunction(MyArg1, MyArg2)
Any of the following function calls are then valid:
MyFunction(Value1, Value2)
MyFunction(Value1, Value2, Value3)
MyFunction(Value1, Value2, Value3, Value4)
MyFunction(Value1, Value2, Value3, Value4, Value5)