/function
/function| Permission level required |
|
|---|---|
| Restrictions |
Cheat only |
{
"title": "<code>/function</code>",
"rows": [
{
"field": "\n* 2 <sup class=\"noprint nowrap Inline-Template \" title=\"\">[<i><span title=\"This statement only applies to Java Edition\">(link to Java Edition article, displayed as JE) only</span></i>]</sup>\n* 1 <sup class=\"noprint nowrap Inline-Template \" title=\"\">[<i><span title=\"This statement only applies to Bedrock Edition\">(link to Bedrock Edition article, displayed as BE) only</span></i>]</sup>",
"label": "(link to Permission level article, displayed as Permission level<br>required)"
},
{
"field": "(link to Options article, displayed as Cheat) only<sup class=\"noprint nowrap Inline-Template \" title=\"\">[<i><span title=\"This statement only applies to Bedrock Edition\">(link to Bedrock Edition article, displayed as BE) only</span></i>]</sup>",
"label": "(link to Commands#Restrictions article, displayed as Restrictions)"
}
],
"invimages": [],
"images": []
}
Runs commands found in the corresponding function file(s) with command execution context (executor, execution position, angles, etc) of the /function command itself.
Usage
In Java Edition, runs a function or all functions in a tag from a data pack.
In Bedrock Edition, runs a function from a behavior pack.
In Java Edition, the behavior of /return run function ... (or /return run execute ... run function ...) is different with a direct call to the /function command. If /return run function ... runs multiple functions, execution stops after first /return in any of the functions. If a direct call to the /function ... runs multiple functions, all of the functions are executed, and the return values are added up as the result value of the /function ... command.
Syntax
- Java Edition
function <name>- Runs a function or functions in a tag.
function <name> <arguments>function <name> with (block <sourcePos>|entity <source>|storage <source>) [<path>]- Runs a function or functions in a tag, with arguments for macros.
- Bedrock Edition
function <name: filepath>- Runs a function.
Arguments
JE: <name>: function
BE: name: filepath: CommandFilePath
- Specifies the function(s) to execute.
- In Java Edition, must be a resource location or one prefixed with a
#, which resolves into a function or a function tag during command execution. In Bedrock Edition, must be of the formatpath/to/function/file, which resolves into a function located at[behavior_pack]/functions/path/to/function/file.mcfunctionduring command execution.
JE: <arguments>: nbt_compound_tag
- Specifies arguments for macro functions in a compound NBT tag.
- Must be a [NBT Compound / JSON Object] compound NBT in SNBT format.
- The position of the target block entity whose NBT is to be used.
- Must be a block position composed of
<X>,<Y>and<Z>, each of which must be an integer or a tilde and caret notation.
JE: <source>: entity (in entity <target> mode)
- Specifies an entity whose NBT is to be used.
- Must be a player name, a target selector or a UUID.
JE: <source>: resource_location (in storage <target> mode)
- Specifies a storage whose NBT is to be used.
- Must be a resource location for an unregistered content.
- Specifies the NBT path. Should be a path of a single compound tag.
- Must be an NBT path.
Result
| Command | Trigger | Java Edition | Bedrock Edition |
|---|---|---|---|
| Any | The command is incomplete, or any argument is not specified correctly. | Unparseable | Unparseable |
| The specified function or function tag does not exist. | Failed | Failed | |
| There's no function in the function tag. | N/A | ||
/function <name> | There's any macro line in the function(s). | Failed | |
/... with block ... | <sourcePos> is unloaded or out of the world. | ||
The block at <sourcePos> is not a block entity. | |||
/... with entity ... | <source> fails to resolve to an entity (named player must be online). | ||
/... with ... <path> | There's no tag at <path>. | ||
More than one tag is selected by <path>. | |||
The tag at <path> is not a compound tag. | |||
/... <arguments> | With the provided arguments, any macro line in the function(s) becomes an unparseable command. | ||
| Any | Only one function is specified, and in it there's no /return command being executed. | Void | |
Only one function is specified, and in it there's a /return command being executed and returning a failure. | Failed | ||
Multiple functions are specified, and in all of the functions, there is no /return command being executed. | Void | ||
| Otherwise | Successful | ||
Output
In Java Edition, for /return run execute store ... run function ...:
| Command | Edition | Situation | Success Count | /execute store success ... | /execute store result ... |
|---|---|---|---|---|---|
| Any | Java Edition | On fail | N/A | 0 | 0 |
| On void | N/A | unchanged | unchanged | ||
| On success | N/A | 1 | the return value of the first non-void function (Execution stops after first /return in any of the specified functions) |
In Java Edition, otherwise:
| Command | Edition | Situation | Success Count | /execute store success ... | /execute store result ... |
|---|---|---|---|---|---|
| Any | Java Edition | On fail | 0 | 0 | 0 |
| On void | 0 | unchanged | unchanged | ||
| On success | 1 | 1 | the sum of the return values of all the non-void functions |
In Bedrock Edition:
| Command | Edition | Situation | Success Count |
|---|---|---|---|
| Any | Bedrock Edition | On fail | 0 |
| On success | 1 |
Examples
- In Java Edition:
- To run a function located at
data/custom/functions/example/test.mcfunctionin a loaded data pack:/function custom:example/test - To run all functions in a function tag located at
data/custom/tags/functions/example/test.json:/function #custom:example/test - To run a function if there is a sheep within a radius of 2 blocks:
/execute if entity @e[type=sheep,distance=..2] run function custom:example/test - To run a function unless the executor is a player:
/execute unless entity @s[type=player] run function custom:example/test - To run a function macro with arguments a=42, b="example":
/function custom:example/test {a: 42, b: "example"} - To run a function macro with arguments from storage
custom:storage:/function custom:example/test with custom:storage
- To run a function located at
History
| Java Edition | |||||||
|---|---|---|---|---|---|---|---|
| 1.12 | pre1 | Added /function. | |||||
| pre4 | Added new arguments to the /function command: [if|unless] [selector] | ||||||
| pre6 | Skipped functions (when a conditional fails) are now considered failures when used in commands. | ||||||
| 1.13 | 17w49b | /function now accept function tags as the argument. | |||||
Removed [if|unless] arguments in favor of /execute [if|unless]. | |||||||
| 1.20.2 | 23w31a | Added function macro arguments. | |||||
| 1.20.3 | 23w41a | /function command no longer returns the number of commands executed to better accommodate new /return command. | |||||
| 23w44a | If /function <function tag> runs multiple functions in combination with /return run, execution stops after first /return in any of the functions. | ||||||
A single call to the /function command always returns when run with /return run. For example, /return run execute [split context] run function <some function with conditional return> always returns after processing the first context. | |||||||
| 1.20.5 | 23w51a | Fixed a bug that /function executing a function with /return fail has a success value of 1.[1][2] | |||||
| Bedrock Edition | |||||||
| 1.8.0 | beta 1.8.0.8 | Added /function. | |||||
See also
- Tag – To run function in batch, every tick, or on server (re)load.