The following rules apply to functions that you define using CFScript or the cffunction tag:
The function name must be unique. It must be different from any existing variable, UDF, or built-in function name, except you can use the ColdFusion advanced security function names.
The function name must not start with the letters cf in any form. (For example, CF_MyFunction, cfmyFunction, and cfxMyFunction are not valid UDF names.)
You cannot redefine or overload a function. If a function definition is active, ColdFusion generates an error if you define a second function with the same name.
You cannot nest function definitions; that is, you cannot define one function inside another function definition.
The function can be recursive, that is, the function definition body can call the function.
The function does not have to return a value.
You can use tags or CFScript to create a UDF. Each technique has advantages and disadvantages.
In respect of first rule. Following code give error as: Entity has incorrect type for being called as a function. <cfscript> function Add(a,b) { Add = a +b; return Add; } </cfscript> Addition of 5 and 4 is <cfoutput>#Add(5,4)#</cfoutput><br /> Addition of 2 and 3 is <cfoutput>#Add(2,3)#</cfoutput>
First time calling Add(5,4) is ok, but inside of Add(a,b) we redefined “Add” as simple variable which is now available on the calling page as simple variable rather than as a function name, and will give error on second call of Add(2,3)
The code will be corrected by two ways: (1) if we use: “var Add = a +b;” instead of “Add = a +b;” (2) or we use different variable name not same as function name like: Addition = a +b; return Addition;
Comments
Ashish-Saxena said on Jul 21, 2007 at 9:21 AM :