Script-Based UDFs

CFScript-based UDFs look a little different than their tag-based siblings. The first thing you should notice is that all script-based UDFs must be written within a cfscript tag block:

<cfscript>
function function_name([arg1][,arg2]...)
{
  CFScript statements
}
</cfscript>

Script-based UDFs begin with the function statement. The name of the function is defined by function_name and follows the same naming conventions as tag based UDFs. Script-based functions can be written to accept zero or more required arguments and any number of optional arguments. Each required argument must be given a name and explicitly declared in the function statement. For example, to declare a function called Mean that accepts a single parameter called Values, you would code it like this:

function Mean(Values)

Within the body of the function, you may use any CFScript statements you wish. Var is used to declare variables that are local to the function. Variables declared with Var must be defined at the top of the function, before any other CFScript statements, and they take precedence over any other variable with the same name, regardless of the variable’s scope. The syntax for declaring a variable using Var in CFScript is:

Var variable = expression;

The Return statement is optional and determines what value(s) to return when the function has finished executing.[4] You may specify any valid expression in the Return statement. If no expression is given in the Return statement, ColdFusion returns ...

Get Programming ColdFusion MX, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.