DevLearningTools

MODULE 6 · LESSON 02

User-Defined Functions

Writing your own functions in ColdFusion — CFScript function syntax, the <cffunction>/<cfreturn> tag equivalent, where a function can live, and the naming rules that trip people up.

New lessons are added one at a time as the course gets built out — a graded quiz for each lesson is still on the way.

A user-defined function (UDF) is just a named, reusable block of code you write yourself — the same idea as a built-in function, except you control what it does. Anywhere the same few lines of logic would otherwise get copy-pasted, a function is almost always the better call.

Learning Objectives

After completing this lesson, you'll be able to:

  • Write a function in CFScript, and the equivalent in tag syntax.
  • Know where a function can be defined so it's reachable from the code that needs it.
  • Use the key <cffunction> attributes: returnType, output, access, hint.
  • Avoid the handful of names ColdFusion won't let you use for a function.

CFScript Function Syntax

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
NOTE

The function body must use curly braces even if it's a single line, and var statements (covered in the Variable Scope lesson) must come before any other code in the body.

Tag Syntax: <cffunction> and <cfreturn>

Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Where a Function Can Live

  • Directly on the calling page (.cfm) — only reachable from that same page.
  • In a file brought in with <cfinclude>, as long as the include happens before the function is called.
  • In Application.cfc or Application.cfm — reachable from every page in the application.
  • As a method inside a ColdFusion Component (.cfc) — covered in the OOP module.

Key <cffunction> Attributes

AttributePurpose
returnTypeThe data type the function returns (string, numeric, array, struct, void, any, ...)
outputWhether the function body can output content directly (rarely needed for logic-only functions)
accessVisibility: public, private, package, or remote
hintA description shown when introspecting the function or component

Naming Rules

Function names must be unique within their scope and can't contain periods. They also can't start with "cf" or reuse a small set of reserved names, including writeDump, writeLog, location, throw, and trace.

Common Beginner Mistakes

Naming a function something starting with "cf"

ColdFusion reserves the "cf" prefix for its own tags and functions — pick a different name to avoid a naming conflict.

Calling a function before the page that defines it has run

A function defined further down the same page, or in a <cfinclude> that hasn't executed yet, isn't callable until ColdFusion has processed that definition. Put shared functions somewhere that's guaranteed to run first, like Application.cfc.

Forgetting the curly braces on a single-line CFScript function

function double(n) return n * 2; is not valid — the body always needs { } even for one line: function double(n) { return n * 2; }

Best Practices

  • Prefer CFScript function syntax for new code — it's shorter and matches JavaScript/Java conventions.
  • Give every function a clear returnType — it documents intent and lets ColdFusion catch type mistakes earlier.
  • Keep functions small and single-purpose — if a function's name needs "and" to describe it, it's probably doing two things.

Interview Questions

What's the tag-syntax equivalent of a CFScript function's return statement?

<cfreturn expr> — it returns a value from a component method or tag-based function the same way return does in CFScript.

Name two places a user-defined function can live besides the calling page itself.

Application.cfc (application-wide) or a ColdFusion Component (.cfc) as a method — a file brought in with <cfinclude> also counts.

Why can't a function be named cfSomething?

ColdFusion reserves names starting with "cf" for its own built-in tags and functions.

Summary

In this lesson, you wrote functions in both CFScript and tag syntax, learned where a function needs to live to be reachable, and covered the key <cffunction> attributes and naming restrictions.

What's Next?

The next lesson digs into arguments — the arguments scope, default values, required vs optional parameters, and how ColdFusion passes different data types into a function.