Minecraft Wiki
Advertisement
For the command, see Commands/function.
Move arrows
It has been suggested that this page be moved to Java Edition function(s).
If this move affects many pages or may potentially be controversial, do not move the page until a consensus is reached.
 [discuss]
Reason: Consistency with other articles that have a Java and bedrock variant; plural should be discussed
When moving, be sure to use the appropriate tool instead of simply copying and pasting the page's contents, to preserve edit history.
Information icon
This feature is exclusive to Java Edition. 

Functions are data pack files, allowing players to run lists of commands. Being text files, they are easily modifiable and less likely than command blocks to induce lag when used in large quantities.

This page covers how functions are defined and invoked in Java Edition.

Definition[]

A function is a text file with the file extension .mcfunction. The recommended encoding for function files is ANSI or UTF-8, without BOM.

As with all other data pack files, the functions folder may also contain subfolders to further organize the function files within a namespace. These subfolders must be referenced when invoking the function.

TestingFunctions

Testing the function system. Three /tellraw messages and one /give command were used in this simple function.

Within the .mcfunction file, one valid command is placed per line, without the usual forward slash (/). Players can add comments within the function text file by beginning a line with #. Multiple spaces between command arguments are not allowed but a line can have tabs and spaces before and after the command.

Allowed commands[]

Individual commands in functions can be longer than the 32,500 character limit in command blocks but the total number of commands run inside a function still obey /gamerule maxCommandChainLength, which is 65,536 commands by default; any commands beyond this limit are ignored.

In a singleplayer or a LAN world, like a command block, a function can run any command that is no more restrictive than permission level 2.

On the default multiplayer software, a function can run any command that is no more restrictive than the permission level prescribed in function-permission-level setting in server.properties.

Invocation[]

Functions can be invoked in several different manners from other data pack files.

Invocation from commands[]

Functions can be invoked using the /function command.

A single function can be invoked by specifying its resource location.

Unlike other data pack files, functions are supported by tags, allowing them to be grouped together. /function also accepts a function tag's resource location, invoking all listed functions.

Invocation from advancements[]

Advancements can run a function once as a reward for completing them. The commands in the function are run through the player who completed the advancement.

Reward functions are called within advancement JSON files using the following format:

{
  "rewards": {
    "function": "namespace:subfolder/function"
  }
}

Invocation from function tags[]

Functions can be grouped together using function tags. Functions in a tag get executed in the defined order, but only the first occurrence of the same function if it occurs multiple times.

In addition, there are two function tags within the minecraft namespace that have special behavior:

  • Functions listed in the minecraft:load tag run when the world is loaded, or when the server is started. Listed functions also run whenever the data pack is reloaded.
    • These functions run before the player joins the world, meaning any target selectors do not find players. Therefore, commands like /tellraw and /title do not appear for any player.
  • Functions listed in the minecraft:tick tag run at the beginning of each tick, repeating every tick.

Note: The first instance of functions tagged minecraft:tick run before minecraft:load runs. This means that when reloading or loading, the order of execution is tick, load, tick, tick, tick, …

When invoked[]

Functions run all their commands in a single tick and other functions called from within also run their commands in the same tick as their parent. Functions use the execution context of whatever called the function. This includes executing entity, as well as execution position, rotation, dimension, and anchor. Contextual parameters are preserved for every command in the function. An /execute command can change the context, but that change does not carry through to any following commands in the same function.


For example:

execute as @p at @s run function foo:bar
# function foo:bar
teleport @s ~ ~5 ~
setblock ~ ~-1 ~ emerald_block
execute at @s run setblock ~ ~-1 ~ diamond_block

When invoked, this function teleports the nearest player 5 blocks up, place an emerald block one block below their original position before the teleport, and place a diamond block one block below their new position after the teleport.

As seen in the above example, contextual parameters can be changed as usual by their respective /execute sub-commands.

History[]

Java Edition
1.12pre1Added functions.
pre3Commands are no longer allowed to begin with a / (forward slash)
Comments can now only be preceded with #; using // is no longer allowed
pre4Added new arguments to the /function command: [if|unless] [selector]
pre6Skipped functions (when a conditional fails) are now considered failures when used in commands.
1.1317w43aCustom functions have been moved into data packs.
17w45aFunctions are now completely parsed and cached on load.
17w49bRemoved /gamerule gameLoopFunction.
Function can now be tagged.
Functions tagged in tick now run every tick in the beginning of the tick.
18w01aFunctions tagged in load now run after (re)loading the datapack.
1.14.4pre4Added function-permission-level to server.properties
Upcoming Java Edition
1.20.223w31aA single backslash \ as the last non-whitespace character of a line now allows a command to be continued on the next line.
Functions can now contain macro lines, making them Function Macros.
1.20.2 Pre-release 1Numbers used as macro arguments are now always inserted without suffixes, regardless of numeric type.

Issues[]

Issues relating to "Function" are maintained on the bug tracker. Report issues there.

References[]


Advertisement