
198
Chapter 10: Writing and Calling User-Defined Functions
Defining functions in CFScript
You define functions using CFScript in a manner similar to defining JavaScript functions. You
can define multiple functions in a single CFScript block.
Note:
For more information on using CFScript, see
Chapter 6, “Extending ColdFusion Pages with
CFML Scripting,” on page 127
.
CFScript function definition syntax
A CFScript function definition has the following syntax:
function
functionName
(
[argName1[, argName2...]]
)
{
CFScript Statements
}
The following table describes the function variables:
The body of the function definition must consist of one or more valid CFScript statements. The
body must be in curly braces, even if it is a single statement.
The following two statements are allowed only in function definitions:
Function variable
Description
functionName
The name of the function. You cannot use the name of a standard
ColdFusion function or any name that starts with “cf”. You cannot use the
same name for two different function definitions. Function names cannot
include periods.
argName1...
Names of the arguments required by the function. The number of arguments
passed into the function must equal or exceed the number of arguments in
the parentheses at the start of the function definition. If the calling page omits
any of the required arguments, ColdFusion generates a mismatched
argument count error.
Statement
Description
var
variableName = expression
;
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 the name cannot include
periods. The initial value of the variable is the result of evaluating
the expression. The expression can be any valid ColdFusion
expression, including a constant or even another UDF.
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 an argument.
Each var statement can initialize only one variable.
You should use the var statement to initialize all function-only
variables, including loop counters and temporary variables.
return
expression
;
Evaluates expression (which can be a variable), returns its value to
the page that called the function, and exits the function. You can
return any ColdFusion variable type.
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: ......