88
Chapter 4: Using Expressions and Pound Signs
Function syntax
The following table shows function syntax and usage guidelines:
All functions return values. In the following example, the
cfset
tag sets a variable to the value
returned by the
Now
function:
<cfset myDate = DateFormat(Now(), "mmmm d, yyyy")>
You can use the values returned by functions directly to create more complex expressions, as in the
following example:
Abs(Myvar)/Round(3.14159)
For more information on how to insert functions in expressions, see
“Using pound signs”
on page 89
.
Optional function arguments
Some functions take optional arguments after their required arguments. If omitted, all optional
arguments default to a predefined value. For example:
•
Replace("Eat and Eat", "Eat", "Drink")
returns "Drink and Eat"
•
Replace("Eat and Eat", "Eat", "Drink", "All")
returns "Drink and Drink"
The difference in the results is because the
Replace
function takes an optional fourth argument
that specifies the scope of replacement. The default value is “One,” which explains why only the
first occurrence of “Eat” was replaced with “Drink” in the first example. In the second example, a
fourth argument causes the function to replace all occurrences of “Eat” with “Drink”.
Expression evaluation and functions
It is important to remember that ColdFusion evaluates function attributes as expressions before it
executes the function. As a result, you can use any ColdFusion expression as a function attribute.
For example, consider the following lines:
<cfset firstVariable = "we all need">
<cfset myStringVar = UCase(firstVariable & " more sleep!")>
When ColdFusion server executes the second line, it does the following:
1
Determines that there is an expression with a string concatenation.
2
Evaluates the firstVariable variable as the string "we all need".
3
Concatenates "we all need" with the string " more sleep!" to get "we all need more sleep!".
4
Passes the string "we all need more sleep!" to the
UCase
function.
Usage
Example
No arguments
Function()
Basic format
Function(Data)
Nested functions
Function1(Function2(Data))
Multiple arguments
Function(Data1, Data2, Data3)
String arguments
Function('This is a demo')
Function("This is a demo")
Arguments that are expressions
Function1(X*Y, Function2("Text"))
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: ......