Switch Statement

As long as you are able to grasp the idea that all of ColdFusion is in tags and understand how to use an if statement, switch statements aren’t too difficult to understand. The expression must be in the output tags, ## and in quotes, of course. The <cfcase> follow the expression, but they don’t need to output a variable so the ## should not be here unless you planning on putting a variable in the numbers place. Just try to think of a switch statement as one really really long or short (depending on how many cases you make) if statement. That is one long if statement with a lot of else ifs. Let’s go to an example.

Example
<cfoutput>
    <cfset x = 3>
    <cfswitch expression="#x#">
        <cfcase value="1">
            My value is 1 <br/>
        </cfcase>
        <cfcase value="2">
            My value is 2 <br/>
        </cfcase>
        <cfcase value="3">
            My value is 3 <br/>
        </cfcase>
        <cfcase value="4">
            My value is 4 <br/>
        </cfcase>
    </cfswitch>
</cfoutput>
Result My value is 3

Let’s break the switch statement down. We have the switch statement put in one big <cfoutput> because we are lazy and that is what lazy people do. Then, we set our variable x equal to 3. After that, we finally come to our switch statement. The <cfswitch expression=”#x#”> is where we define what variable we are going to check the cases (or ifs) against. Using our variable x, we run through the first case <cfcase value = “1”>, which is simply asking is our variable x equal to one? If it is, do everything before the closing </cfcase>. If not, skip the current <cfcase> and check the next one. So, we fail the first few cases and come to the <cfcase value=”3″>. We meet the condition and so we output “My value is 4 <br />”. Finally, we exit the switch statement. Tutorial mastered!

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.