cfscript-Based UDFs


<cfscript>-Based UDFs

<cfscript>-based UDFs are defined using the function keyword; return values are specified using return.

<cfscript> was reviewed in Chapter 21, "Scripting."


Creating UDFs

UDFs are created using function within a <cfscript> block. Here is the <cfscript> equivalent of the Tomorrow() function seen previously:

 <cfscript> function Tomorrow() {  return DateAdd("d", 1, Now()); } </cfscript> 

Again, Tomorrow() returns tomorrow's date (calculated by adding 1 day to today's date). The function is named using scripting syntax, just as it would be done in JavaScript, and the return value is specified using return.

Processing Arguments

<CFSCRIPT>-based UDFs can also accept arguments, but with a very important limitation. When you create UDFs in <cfscript>, arguments are enumerated in the function definition. But there is no way to specify a data typeregardless of whether the argument is requiredor specify the default values if needed. You'll need to do that processing yourself. Here is the Cube() example in <cfscript>:

 <cfscript> function Cube(num) {  // return the cube  return num*num*num; } </cfscript> 

Cube() returns the cube of a passed number. From a required argument (the number to be cubed) it returns the cube value. What makes the argument required is the fact that it is enumerated in the function declaration. In <cfscript>-based UDFs, all listed arguments are required.

Local Variables

Local variables can be created in <cfscript>-based UDFs too. To create a local variable, simply place var in front of the variable definition, like this:

 var result="no"; 



Macromedia ColdFusion MX 7 Certified Developer Study Guide
Macromedia ColdFusion MX 7 Certified Developer Study Guide
ISBN: 0321330110
EAN: 2147483647
Year: 2004
Pages: 389
Authors: Ben Forta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net