Home » Tutorials » ColdFusion » Functions

Functions

In ColdFusion, functions are probably the best way you can make your code readable, and keep the scope at the level you want. Remember to add a useful comment before each function. It also prevents you from using variables with the same name (not that you should have variables with the same name). In ColdFusion variables declared inside the function are local variables, which means they cannot be called outside the function. Functions do absolutely nothing until they are called. Generally, you should try to put your functions above your outputting code to separate your logic from your output (views). Let’s go through one together.

Example
<cffunction name="addingFunction" access="public" returnType="numeric">
    <cfargument name="firstNumber" type="numeric" required="true">
    <cfargument name="secondNumber" type="numeric" required="true">
    <cfset var sumOfThem = arguments.firstNumber + arguments.secondNumber>
    <cfreturn sumOfThem>
</cffunction>
<cfoutput>
    #addingFunction(2,3)#
</cfoutput>

The name=”addingFunction” sets the name of the function so we can call it later. The access sets the scope of the function, and returnType just specifies what type of value you will be returning. On to the arguments, we can see that there are two <cfargument> tags (With no closing tag). The arguments both have a name, a type, and a required attribute. You can probably guess what each of them do. The name is just the name of the argument that you can call later in the function. The type attribute is very important because ColdFusion gets grumpy if you pass in the wrong variable type, which makes a lot of sense. No one wants a function trying to parse random variables. You should know what argument types your function takes if you are going to be using it. Finally, the required attribute lets ColdFusion know if it is an optional parameter.

Next, we do the actual action of the function. We set a variable called sumOfThem equal to our first argument, arguments.firstNumber, plus our second argument, arguments.secondNumber. Pay attention to the fact that we used var before our variable name. We are doing this so that we create a new variable even if one exists outside of this function. Finally, we use the <cfreturn> tag to return our result to where ever we called the function. Notice how we add the arguments. before the actual argument name. This is just so we don’t use another variable floating around. Always try to control your variable names when you are getting their value. A little extra code won’t kill you and will prevent some future errors.

Let’s walk through the function. #addingFunction(2,3)# calls the function and passes in 2 as the first argument. So, arguments.firstNumber now equals 2. Since we also passed 3 into the function, the arguments.secondNumber now equals 3. After we have set the arguments in the function, we add them when we set the sumOfThem variable. Finally, we return that variable, and the <cfoutput> tags writes out 5 because our function #addingFuction(2,3)# returned 5.

Functions are awesome, use them.



Link/cite this page

If you use any of the content on this page in your own work, please use the code below to cite this page as the source of the content.

  • Stewart, Suzy. "Functions". After Hours Programming. Accessed on March 16, 2024. https://www.afterhoursprogramming.com/tutorial/coldfusion/functions-cf/.

  • Stewart, Suzy. "Functions". After Hours Programming, https://www.afterhoursprogramming.com/tutorial/coldfusion/functions-cf/. Accessed 16 March, 2024.

  • Stewart, Suzy. Functions. After Hours Programming. Retrieved from https://www.afterhoursprogramming.com/tutorial/coldfusion/functions-cf/.



Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.