DevLearningTools

MODULE 6 · LESSON 04

Return Values

Returning data from a ColdFusion function — the return statement, <cfreturn>, the returnType attribute, functions with no return value, and returning more than one value with a struct.

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 function's return value is how it hands data back to whatever called it. CFScript uses return, tag syntax uses <cfreturn> — both do the same thing, and both immediately end the function's execution the moment they run.

Learning Objectives

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

  • Return a value from a function in both CFScript and tag syntax.
  • Set an accurate returnType, including void for functions that don't return anything.
  • Return more than one value by bundling them into a struct.
  • Recognize what happens when a function has no return statement.

return and <cfreturn>

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

return / <cfreturn> ends the function immediately — any code written after it in the same branch never runs.

Returning from Different Branches

A function can have more than one return statement, as long as only one of them runs on any given call — each conditional branch returns its own result.

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

returnType, Including void

returnType documents (and, in tag syntax, enforces) what a function hands back. For a function that only causes a side effect — writing a log entry, sending an email — and returns nothing, use returnType="void" and skip return entirely.

CFScript
<cfscript>

void function logMessage(msg) {
    writeLog(text = msg, file = "app");
}

</cfscript>
Tag Syntax
<cffunction name="logMessage" returnType="void">
    <cfargument name="msg" type="string" required="true">
    <cflog text="#arguments.msg#" file="app">
</cffunction>

Returning More Than One Value

A function can only return a single value — to hand back several related pieces of data at once, bundle them into a struct.

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

Common Beginner Mistakes

Writing code after a return statement and expecting it to run

return (or <cfreturn>) exits the function immediately — anything after it in the same branch is dead code and never executes.

Leaving returnType as its default (any) for every function

any always works, but a specific returnType documents what callers can expect and helps catch mistakes sooner — use it whenever the return type is actually fixed.

Trying to return two separate values as two return statements

Only the first return statement reached ever runs. To hand back multiple values, bundle them into a single struct and return that.

Best Practices

  • Set returnType="void" on functions that only cause a side effect and don't hand back data.
  • Prefer several early returns for distinct conditions over one deeply nested if/else building a single final value.
  • Return a struct with clearly named keys rather than an array when returning multiple values — array[1]/array[2] is much less self-documenting than result.quotient.

Interview Questions

What happens to code written after a return statement in the same branch?

It never executes — return / <cfreturn> ends the function's execution immediately.

How do you return more than one value from a ColdFusion function?

Bundle the values into a struct (or another compound type) and return that single struct — a function can only return one value.

What returnType should a function use if it doesn't return anything?

void — it documents that the function is called only for its side effects.

Summary

In this lesson, you covered return and <cfreturn>, functions with multiple conditional return branches, the void returnType for side-effect-only functions, and bundling multiple values into a struct to return them together.

What's Next?

The next lesson covers variable scope — the VARIABLES and LOCAL scopes, the var keyword, and ColdFusion's full scope-lookup order, including a real security gotcha around unscoped variables.