Whatever message this page gives is out now! Go check it out!
function twoPower(exponent) {
return 2^exponent;
}
</cfscript>{
CFScript Statements
}Function variable | Description |
functionName | The name of the function. You cannot use the name of a standard ColdFusion function or any name that starts with "cf". You cannot use the same name for two different function definitions. Function names cannot include periods. |
argName1... | Names of the arguments required by the function. The number of arguments passed into the function must equal or exceed the number of arguments in the parentheses at the start of the function definition. If the calling page omits any of the required arguments, ColdFusion generates a mismatched argument count error. |
Statement | Description |
varvariableName = expression; | Creates and initializes a variable that is local to the function (function variable). This variable has meaning only inside the function and is not saved between calls to the function. It has precedence in the function body over any variables with the same name that exist in any other scopes. You never prefix a function variable with a scope identifier, and the name cannot include periods. The initial value of the variable is the result of evaluating the expression. The expression can be any valid ColdFusion expression, including a constant or even another UDF.All var statements must be at the top of the function declaration, before any other statements. Initialize all variables when you declare them. You cannot use the same name for a function variable and an argument.Each var statement can initialize only one variable.Use the var statement to initialize all function-only variables, including loop counters and temporary variables. |
returnexpression; | Evaluates expression (which can be a variable), returns its value to the page that called the function, and exits the function. You can return any ColdFusion variable type. |
function Sum(a,b) {
var sum = a + b;
return sum;
}
</cfscript>function MySum(a,b) {Return a + b;}<cfargument name="exponent">
<cfreturn 2^exponent>
</cffunction>access="accessType" output="Boolean"]>
<cfargument name="argumentName" [Type="type" required="Boolean"
default="defaultValue">]
<!--- Function body code goes here. --->
<cfreturn expression>
</cffunction>Attribute | Description |
name | The function name. |
returnType | (Optional) The type of data that the function returns. The valid standard type names are: any, array, binary, Boolean, date, guid, numeric, query, string, struct, uuid, variableName, xml, and void. If you specify any other name, ColdFusion requires the argument to be a ColdFusion component with that name. ColdFusion throws an error if you specify this attribute and the function tries to return data with a type that ColdFusion cannot automatically convert to the one you specified. For example, if the function returns the result of a numeric calculation, a returnType attribute of string or numeric is valid, but array is not. |
roles | (Optional) A comma-delimited list of security roles that can run this method. If you omit this attribute, ColdFusion does not restrict user access to the function.If you use this attribute, the function executes only if the current user is logged in using the cfloginuser tag and is a member of one or more of the roles specified in the attribute. Otherwise, ColdFusion throws an unauthorized access exception. For more information on user security, see Securing Applications. |
output | (Optional) Determines how ColdFusion processes displayable output in the function body.If you do not specify this option, ColdFusion treats the body of the function as normal CFML. As a result, text and the result of any cfoutput tags in the function definition body are displayed each time the function executes.If you specify true or yes, the body of the function is processed as if it is in a cfoutput tag. ColdFusion displays variable values and expression results if you surround the variables and expressions with number signs (#).If you specify false or no., the function is processed as if it is in a cfsilent tag. The function does not display any output. The code that calls the function is responsible for displaying any function results. |
Attribute | Description |
name | The argument name. |
type | (Optional) The data type of the argument. The type of data that is passed to the function. The valid standard type names are any, array, binary, Boolean, date, guid, numeric, query, string, struct, uuid, and variableName. If you specify any other name, ColdFusion requires the argument to be a ColdFusion component with that name. ColdFusion throws an error if you specify this attribute and the function is called with data of a type that ColdFusion cannot automatically convert to the one you specified. For example, if the argument type attribute is numeric, you cannot call the function with an array. |
required | (Optional) A Boolean value that specifies whether the argument is required. If set to true and the argument is omitted from the function call, ColdFusion throws an error. The default value is false. The required attribute is not required if you specify a default attribute.Because you do not identify arguments when you call a function, all cfargument tags that specify required arguments must precede any cfargument tags that specify optional arguments in the cffunction definition. |
default | (Optional) The default value for an optional argument if no argument value is passed. If you specify this attribute, ColdFusion ignores the required attribute. |
<cfargument name="empID" required="true" type="numeric">
<cfset var cfdocexamples="">
<cfquery dataSource="cfdocexamples" name="deptID">
SELECT Dept_ID
FROM Employee
WHERE Emp_ID = #empID#
</cfquery>
<cfreturn deptID.Dept_ID>
</cffunction><cfscript>
function hypotenuse(a, b) {
function square(x) { return x*x; }
return sqr(square(a) + square(b));
}
result = hypotenuse(1,2);
writeoutput(result);
</cfscript><!---hypotenuse.cfc--->
<cfcomponent>
<cffunction name="calchypotenuse">
<cfargument name="a" required="true"/>
<cfargument name="b" required="true"/>
<cffunction name="square">
<cfargument name="x" required="true"/>
<cfreturn x*x/>
</cffunction>
<cfreturn sqr(square(a) + square(b))/>
</cffunction>
</cfcomponent><!---hypotenuse.cfm--->
<cfset myhypotenuse=new hypotenuse()>
<cfoutput>#myhypotenuse.calchypotenuse(1,2)#</cfoutput>function doSomething(){
// ...do something...
}doSomething = function(){
// ...do something...
}(function(){
// ...do something...
})()(function(){
// ...do something...
})(function(){
// ...do something...
})()<cfscript>
// function Definition
function addTogether() {
x = 20
y = 20
answer = x + y
writeOutput(answer)
}
// function invocation
addTogether()
</cfscript><cfscript>
// closure Definition
func = function () {
x = 20
y = 20
answer = x + y
writeOutput(answer)
}
// closure invocation
func()
</cfscript><cfscript>
(function addTogether() {
x = 20
y = 20
answer = x + y
writeOutput(answer)
})()
</cfscript><cfscript>
// Normal function expression
function printOutput(){
writeOutput("Hello")
}
printOutput()
//Using IIFE
writeoutput((function(name) {
return( "Hello #name#" );
})("Darkness, my old friend!<br>"));
</cfscript><cfscript>
// IIFE
(function(name) {
writeOutput("Hello " & "#name#")
})("Joker<br/>");
// named function
(function printName(name) {
writeOutput("Hello " & "#name#")
})("Batman<br/>");
</cfscript><cfscript>
// using lambda
((name)=> {
writeOutput("Hello " & "#name#")
})("Joker<br/>");
// lambda- two parameters
((fName,lName)=>{
writeOutput("Hello " & fName & " " & lName)
})("Bruce","Wayne")
</cfscript><cfscript>
// simple statement
(()=>{
writeOutput("Hello IIFE")
})()
// assignment statement
x=(()=>2)()
writeOutput("<br/>" & x)
</cfscript><cfscript>
// ternary operator
((a,b)=>{
c = (a < b) ? a : b
writeOutput(c)
})(20,10)
// unary operator
unOp=(()=>13)()
writeOutput(unOp)
</cfscript><cfscript>
// binary operator
// AND
((a,b)=>{
c=a && b
writeOutput("a && b is: " & c)
})(6,1)
// OR
((a,b)=>{
c=a || b
writeOutput("a || b is: " & c)
})(6,1)
// XOR
((a,b)=>{
c=a XOR b
writeOutput("a xor b is: " & c)
})(6,1)
</cfscript><cfscript>
// simple assignment
(()=> {writeOutput("This is a simple assignment<br/>")})()
// multi assignment
d = 3
a=b=c=d*(() => 5)()
writeOutput(a)
</cfscript><cfscript>
// single param, single statement
writeOutput((name => "Hello, #name#")("Joker"))
// multiple params, single statement
func=(( name, lastname ) => "Hello, #name# #lastname#")("Tony", "Stark")
writeOutput(func)
// multiple params, multi statement
func1=(( name, lastname ) => {
return "Hello, #name# #lastname#"
})("Jimmy", "Page")
writeOutput(func1)
</cfscript><cfscript>
// As a return statement in UDF
function plusTwo(num) {
return (() => num + 2)()
}
writeOutput(plusTwo(5) & "<br>")
// As a param to built-in / UDF methods
myStringVar = UCase((() => " more sleep!")())
writeOutput(myStringVar)
</cfscript><cfscript>
// if-else statement
if((function () {return "orange"})() == "banana"){
writeOutput("Fruit is orange!" & "<br/>")
}
else{
writeOutput("Fruit is something else")
}
</cfscript><cfscript>
// recursive IIFE
writeoutput(((param) => param + 15)((() => 13)()))
</cfscript><cfscript>
// for loop
for (i=1;i<=(()=>5)();i++){
writeOutput(i & ". " & "Hello World" & "<br/>")
}
</cfscript><cfscript>
// Array implicit initialization
arr = [
(()=> 1)(),
(()=> 2)(),
(()=> 3)()
]
writeDump(arr)
// Struct impicit initialization
str={
"a":(()=>1)(),
"b":(()=>2)(),
"c":(()=>3)()
}
writeDump(str)
</cfscript>