Function (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.
* (data pack name)
** pack.mcmeta
** pack.png
** data
*** (namespace)
**** advancements
***** (advancement).json
**** functions
***** (function).mcfunction
**** item_modifiers
***** (modifier).json
**** loot_tables
***** (loot table).json
**** predicates
***** (predicate).json
**** Further folders…
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.
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 , 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 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 and 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 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 sub-commands.
== History ==
== Issues ==
== References ==
de:Funktion
es:Función (Java Edition)
fr:Fonction
ja:関数 (Java Edition)
pl:Funkcja
pt:Funções
ru:Функция
uk:Функція
zh:函数（Java版）