To create a user-defined function all you have to do is use the <cffunction>
tag. For example:
<cffunction name="MakeCapitalised"> <cfargument name="wordToCapitalise" type="string"> <cfset FirstLetter = Left(wordToCapitalise,1)> <cfset FirstLetter = UCase(FirstLetter)> <cfset RestOfWord = Mid(wordToCapitalise, 2, Len(wordToCapitalise)-1)> <cfreturn FirstLetter & RestOfWord> </cffunction> <cfoutput> #MakeCapitalised("steven")# </cfoutput>
The previous code will return the following:
Let's work through the code, we first define the function with a name of MakeCapitalised:
<cffunction name="MakeCapitalised">
Then we define the argument that it takes; in this case, we call it wordToCapitalise
and say that it is of type string:
<cfargument name="wordToCapitalise" type="string">
Next, we get the first letter of the wordToCapitalise
using the Left()
function...