Functions ColdFusion Help

Now that you know how to use variables in your ColdFusion templates you need to consider how to leverage them in different ways including using functions and by .leveraging the power of expressions.

In this chapter you will focus on the latter: functions. Functions are common to many programming languages and provide a way to isolate programming code into a virtual black box that given data as input, performs an expected action and returns the results as output. In the ColdFusion context a large number of built-in functions that provide a broad range of functionality are available to developers.

You will take a quick look at exactly which functions and how to use them in ColdFusion and then at the major groups of functions available in ColdFusion.

Understanding Functions

functions have long been part of the vocabulary of most development environments. Functions offer a way to encapsulate a set of program code that performs a specific function on input data and provides the result as output. The function can then be assigned a name and simply referred to by that name. In effect they are named procedures that perform a distinct purpos:)

Therefore instead of rewriting the same program code each time the particular task neds to be performed, a programmer can simply refer to the function by name and need not be concerned with the specifics of how the function performs the requested task.

Let’s consider an example: taking the cube of a number. Written in plain mathematical notation, you could write:

number x number x number

Which could be used each time you want to perform the cube of a number. Alternately you could encapsulate this detail into a function called. Cube and simply call it:


Of course, this doesn’t seem to be a big benefit. After all, using the first option in your templates doesn’t make them notably more complex than the latter. However, what if you wanted to compute an arbitrary power of a number (such as 3 to the power of 8y or 9 to the power of 7)? You can write program code to do this each time you need to but that requires several lines of code, and the purpose of the code is not apparent at a glance while reading the template code.

Instead, the several lines needed to calculate an arbitrary power could be placed in the function Power and 3 to the power of 8 could then be executed with:

This is far more suggestive of its purpose than several lines of obscure code.

Many programming environments offer built-in functions as well as providing the developer the ability to create their own functions. This is not the case with ColdFusion. Instead, ColdFusion offers a rich library of built-in functions covering almost every aspect of working with data in ColdFusion The very nature of ColdFusion really precludes the need for developers to create-tfieir own functions. Instead, this ability can be achieved through the use of custom tags (see Chapter 29, “Building ColdFusion Custom Tags”). Although custom tags are not identical to functions, the same results can be achieved with both

Using Fundions

Using ColdFusion functions your templates is not a complicated process. There are -two simple aspects of functions to understand: how to provide information to functions and how to use the results.

Functions generally need input data to perform their work. This input is comm only known as parameters cSr arguments. As you will see by looking through the !functions in Appendix C “Cold Fusion Function Reference,” functions are defined to take these parameters in a specified order.

In Coldfusion the syntax of passing arguments to functions is

Therefore in our preceding Power example the Power function takes two arguments (the base and the exponent):

All functions return a single value. This value can be of any data type (a number a string, or even a more complex data .type such as an array which is discussed in Chapter 14, “Working with ColdFusion Data Structures”).

Basically the function call itself can be used where the data type returned can be used. For instance our example Power function returns a number value so it can be used wherever a number can be used:

• In a variable assignment: <CFSET Result – Power(3,8»
• In an expression (see Chapter 6, “Writing Expressions”): 2 + Power(3, 8)
• As an argument to another function: Cube(Power(3, 8))
• In dynamic output: <CFOUTPUT>#Power(3,8)#</CFOUTPUT>

Of course, functions aren’t that useful if you can pass only literal values to them as parameters. What makes them more useful is that variables expressions and other functions can be passed as arguments.

Consider the following example:

<CFSET Base-3>
ET Exponent-8>

Here, you use two variab’es as arguments t e function. This is a powerful ability because *e data for the variables can be set anywhere in the template prior to the function call. You can even use variables derived from external information such as query results, form submissions, or URL parameters .

.It is also useful to be able to pass an expression to a function. For instance, the following example uses an expression to add two variables and pass them as an argument to the function:

<CFSET Base-3>
<CFSET Exponentl-6>
<CFSET Exponent2-2>
<CFOUTPUT>lPower(Base, Expgnentl+Exponent2)j</CFOUTPUT>

Effectively, this function call is the same as Power(3,8).

Reviewing the Use of Functions in ColdFusion

So far in this chapter we have used our example Power function to illustrate the concepts behind functions and how to use them. However Power is a function created for the purposes of this chapter. While Power is not a genuine ColdFusion function Cold Fusion does offer many functions for a variety of purposes including the following

• Manipulating arrays
• Working with dates and times
• Making decisions
• Formatting output
• Manipulating lists
• Working with structures
• Performing mathematical calculations
• Manjpulating strings

Manipulating Arrays

Arrays are complex data structures available in ColdFusion. A large number of functions are available for working with Cold Fusion arrays. We discuss arrays and the’ functions for manipulating them in depth in Chapter 14 “Working with Cold fusion Data Structures.”

Working with Date sand Time

ColdFusion offers a variety of functions for manipulating dates and times. These include functions for creating date objects; for outputting specific information about a date including the day of the week, the name of the monthand a user-friendly format for the date as well as for adding or subtracting time from a date.

The date functions are outlined in Appendix C.

Making Decisions

ColdFusion offers several functions that make it easy to determine among other things the data type of a value. For instance it is’possible to test if a value is an array Boolean value date, numeric value query or simple value. You can also use decision functions to check whether a given year is a leap year and to determine whether a user is authenticated and authorized.

The decision functions are outlined in Appendix C

Formatting Output

With;:ll these data types it is useful to be able to easily format different types of data for. attractive, user-friendly output. Several functions assist developers in outputting consistently formatted numbers (for instance, as currency values), to output dates in a variety of styles and to handle outputting of HTML text in different ways.

The formatting functions are covered in Chapter 12, “Grouping Nesting and Formatting Output

Manipulating Lists

Lists are complex data structures available in ColdFusion. A large number of functions are available for working with ColdFusion lists. We discuss lists in depth in Chapter 14.

Working with Structures

A structure is another complex data structure available in ColdFusion. A large number of functions are available for working with ColdFusion structures. We discuss structures and their related functions in depth in Chapter 14

Performing Mathematical Calculations

As you will see in the next chapter expressions offer the ability to perform all the basic mathematical calculations such as addition multiplication and division

• Absolute values
• Bit-level operations
• Trigonometric calculations
• Logarithmic functions
• Rounding
• Squares

The mathematical functions are outlined in Appendix B.

Manipulating Strings

ColdFusion offers many functions for performing a range of tasks on string values including the following:

• Comparison
• Searching
• Replacing
• Inserting characters
• Removing characters
• Justifying
• Changing case

The string functions are outlined in Appendix B.

Where Do We Go from Here?

Now that you know how to work with variables and functions, you are ready to learn how to use a fundamental building block of programming languages such as ColdFusion expressions.

Expressions enable you to perform complex calculations, make decisions and generally work with data in useful ways. In the next chapter you will look at the types of expressions available in ColdFusion including numeric, string, and Boolean expressions you will learn the intricacies of using the pound sign (#) in expressions; and you will look at how to use literal values variables and functions within your expressions .

Posted on November 14, 2015 in Getting Data From a Database

Share the Story

Back to Top