Creating Functions


You can create functions to perform some specific processes in ColdFusion. These custom functions are known as User-Defined Functions (UDF). You can use ColdFusion tags as well as CFScript code to create these functions.

Note

The <cffunction> tag creates a function in a ColdFusion page.

Creating Functions Using CFScript

You can create a function in CFScript using the function statement. A function contains expressions and statements. You can call a function from another function.

Note

You cannot use CFML tags inside a CFScript function.

The syntax of a CFScript function is

 function function_name ( [argument1], [argument2]) {     [statements];     return [expression]; } 

In the preceding code syntax:

  • function_name is the name of the function to be created.

  • [argument1], [argument2] are the arguments or parameters passed to the function.

  • return [expression] returns the value of an expression evaluated within the function.

The following code uses the function statement to create testfunction() in CFScript:

 <cfscript> function testfunction(myvar) {     WriteOutput ("Multiplying the integer parameter by 2" & (myvar *2) ); } </CFSCRIPT> 

Creating Function Variables

You can create variables inside a function for temporary processing. These variables are local to a function and cannot be accessed outside the scope of that function. You can create a function variable using the var statement, as shown in this code:

 <cfscript> function testfunction(myvar) {     var func_var = myvar * 2;     WriteOutput ("Multiplying the integer parameter by 2 :" & func_var ); } </CFSCRIPT> 

Calling a Function

You can call a function in a CFScript script by using its name. For example, the following code creates and calls a CFScript function that takes two string parameters and compares them for equality:

 <HTML> <HEAD></HEAD> <BODY> <cfscript> function testfunction(stringvar1, stringvar2) {     if (stringvar1 EQ stringvar2) {     Writeoutput("Both strings <b>[" & stringvar1 &" ]["& stringvar2& "]</b>       are absolutely same<br>"); } else {     Writeoutput("Both strings <b>[" & stringvar1 &" ]["& stringvar2& "]</b>       are different<br>"); } } </CFSCRIPT> <cfscript> // Verifying Two similar strings testfunction("This is a sample","This is a sample"); // Verifying Two different strings testfunction("This is a sample","This is not a sample"); </CFSCRIPT> </BODY> </HTML> 

Note

Function parameters are also called arguments. A parameter in the context of a function contains values that are processed by code statements in the function.

Returning Values from a Function

This statement can be used inside a function to return a value from the function. The returned value can be the value of a variable defined in the function or the result of an expression. The following code shows how to use the return statement within a function to return a TRUE or FALSE value:

 <HTML> <HEAD></HEAD> <BODY> <cfscript> function testfunction(stringvar1, stringvar2) {     var myresult= false;     if (stringvar1 EQ stringvar2)     {       myresult= true;     }     return myresult; } </CFSCRIPT> <cfscript> // Verifying Two similar strings if (testfunction("This is a sample","This is a sample")) { //If the testfunction returns a True value Writeoutput("Both strings are similar"); } else { //If the testfunction returns a False value Writeoutput("Both strings are different"); } </CFSCRIPT> </BODY> </HTML> 




Macromedia ColdFusion MX. Professional Projects
ColdFusion MX Professional Projects
ISBN: 1592000126
EAN: 2147483647
Year: 2002
Pages: 200

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