Stop the function and hand a number back to the caller.
/return only matters inside a datapack function: it ends the function early and decides the value the caller sees. The value form exists since 1.20.2; fail and run were added in 1.20.3.
A 32-bit integer. By convention 1 means success or true and 0 means false, but any number works: wave counts, prices, damage totals. The caller reads it with execute store result.
Stops the function immediately and hands this value to the caller: execute store result receives exactly this number. Needs permission level 2 and only does something useful inside a function.
Every function call produces a value. Without /return it is just the number of commands that ran successfully inside the function; with /return you choose it. End the function with /return, then call it through execute store on the caller's side to capture the number in a scoreboard, a bossbar, or NBT.
The /return command ends a datapack function early and decides what value the function hands back to whoever called it. It is how a function says it succeeded with a specific number, or failed outright, instead of just running out of lines.
Every function call in Minecraft produces a value. Before /return existed, that value was always the number of commands that ran successfully inside the function, which is rarely a number anyone wants. /return, added in 1.20.2, replaces it with a value you choose: an explicit integer, a hard failure, or the live result of another command via return run.
The command also works as an early exit. The moment a /return line executes, the rest of the function is skipped, including the remaining iterations when the line was forked over several entities by execute as. That makes guard clauses possible: check a condition at the top of the function, return fail if it does not hold, and write the rest of the function as the happy path. Like the function command itself, /return needs permission level 2.
The command has three forms, all covered by the generator above:
The nested command after run can be anything, including a full execute chain, and is written without a leading slash. Its result value is whatever that command would store through execute store result: data get returns the value it read, execute if entity returns the number of matches, kill returns how many entities died. If the nested command fails, the function fails too, exactly as if it had hit return fail.
Four function lines and one caller you can copy and adapt. Replace the mypack and arena ids with your own datapack's namespace:
/return 1The classic true. Put it at the end of a check function so callers can treat the function like a condition that passed.
/return failAbort early. Everything after this line is skipped and the function call reports failure, so execute if function does not pass.
/return run execute if entity @e[type=zombie,distance=..32]A one-line predicate function: returns the number of zombies within 32 blocks and fails when there are none.
/return run data get entity @s HealthReturns the executor's health as the function's value. Any command that produces a result can be forwarded this way.
/execute store result score #waves arena run function arena:next_waveThe caller's side: whatever number arena:next_wave passes to /return lands in the #waves score on the arena objective.
A return value is only useful if the caller captures it. The standard pattern has three steps:
1. Write a function that ends in /return, for example a mypack:count_players function whose last line is return run execute if entity @a[distance=..50].
2. Call it through execute store: /execute store result score #nearby temp run function mypack:count_players. The returned number lands in the score, but the same store clause can write into a bossbar value or an NBT path instead.
3. Branch on the outcome. Read the score with execute if score, or skip the scoreboard entirely: since 1.20.3, execute if function mypack:count_players runs the function and passes only when it returns success.
This is what turns .mcfunction files into real functions with exit codes. A door controller can ask a permission function whether a player may enter, an arena loop can ask a wave function how many mobs it spawned, and a minigame can chain checks where any return fail stops the whole sequence. One function, written once, answers the same question for every caller.
It ends the current datapack function early and decides the value the function hands back to its caller. return 5 stops the function and returns the number 5, return fail stops it and marks it failed, and return run <command> runs one command and uses that command's result as the function's own. It needs permission level 2 and exists in Java Edition since 1.20.2.
Call the function through execute store. For example /execute store result score #out temp run function mypack:get_count writes whatever value the function passes to /return into the #out score. If the function never runs /return, you get the number of commands that executed successfully inside it instead, so end value-producing functions with an explicit return.
return 0 is a success that happens to carry the value 0: execute store result writes 0 and execute if function passes. return fail is a real failure: there is no result value, execute store leaves the score untouched and execute if function does not pass. Use 0 when zero is a meaningful answer and fail when the function could not do its job.
Yes. As soon as /return runs, the function stops and control goes back to the caller; the lines after it never run, and since 1.20.3 even the remaining iterations of a forked execute as line in the same function are cancelled. It only exits the function it appears in, so a calling function continues normally with the returned value.
/return <value> was added in Java Edition 1.20.2 (snapshot 23w31a). The fail and run forms arrived in 1.20.3 (snapshot 23w40a), which also tightened how return interrupts the rest of the function. Bedrock Edition does not have a /return command.
It parses anywhere with permission level 2, but it is designed for .mcfunction files, where there is a caller to receive the value. Typed in chat the value has nowhere to go, so nothing useful happens. All the practical patterns involve calling a function through execute store result or execute if function and ending that function with /return.
Building the call side too? Or browse more Minecraft tools: