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. The recommended encoding for function files is ANSI encoding (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&hellip;

As with all other data pack files, the  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  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 will still obey, which is 65,536 commands by default; any commands beyond this limit will be 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  setting in.

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. 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.

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


 * Functions listed in the  tag will run when the world is loaded, or when the server is started. Listed functions will also run whenever the data pack is reloaded.
 * These functions run before the player joins the world, meaning any target selectors will find no players. Therefore, commands like and  will not show to any player.
 * Functions listed in the  tag will run at the beginning of each tick, repeating every tick.

Note: The first instance of functions tagged  will run before   runs. This means that when reloading or loading, the order of execution is,  ,  ,  ,  , &hellip;

When invoked:
Functions will run all their commands in a single tick and other functions called from within will also run their commands in the same tick as their parent. Functions will 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 all the commands in the function unless explicitly changed by.

For example:

execute as @p at @s run function foo:bar

teleport @s ~ ~5 ~ setblock ~ ~-1 ~ emerald_block execute at @s run setblock ~ ~-1 ~ diamond_block
 * 1) function foo:bar

When invoked, this function will teleport 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.