ColdFusion 9.0 Resources |
Using functions as operatorsFunctions are a form of operator. Because ColdFusion functions return values, you can use function results as operands. Function arguments are expressions. For example, the following are valid expressions:
Function syntaxThe 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 number signs. Optional function argumentsSome functions take optional arguments after their required arguments. If omitted, all optional arguments default to a predefined value. For example:
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 functionsIt 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:
ColdFusion completes steps 1-3 before running the function. |