How To Use The Function Command Wiki Guide
This Minecraft tutorial explains how to use the /function command with screenshots and step-by-step instructions.
You can use the /function command to run a function in Minecraft. This is a great way to chain multiple commands without using a command block. Let’s explore how to use this cheat (game command).
Requirements
To run game commands in Minecraft, you have to turn cheats on in your world.
Function Command
Function Command in Minecraft Java Edition (PC/Mac)
In Minecraft Java Edition (PC/Mac), there are different syntaxes depending on the conditions that will run the function.
To always run a function (with no conditions):
/function <name>
To run a function only if the selector is matched:
/function <name> [if <selector>]
To run a function if the selector does not match:
/function <name> [unless <selector>]
Definitions
- name is the function to run and must be formatted as:
newFolder:filename
- selector is optional. It is a target selector that is used to describe either players or entities in the game. A selector must be included when preceeded byÂ
if
 orÂunless
.
Where is the Function File Stored in Java Edition?
Each function belongs to a specific saved world.
When storing functions, you must first create a new folder under the data\functions\
 folder for the saved world. You can then store your function within this new folder. The filename for the function must have a .mcfunction extension.
For example, you could create a new folder called 9Minecraft
 for the saved world called World1. You would then store the function give_diamond.mcfunction
 in this new folder. Depending on your version of Minecraft, this function would be saved under one of the following hidden paths (Note: path may be different on your system):
Path for Windows
C:\Users\<username>\AppData\Roaming\.minecraft\saves\World1\data\functions\9Minecraft\give_diamond.mcfunction
Path for Mac
/Users/<username>/Library/Application Support/minecraft/saves/World1/data/functions/9Minecraft/give_diamond.mcfunction
You could then run the file called give_diamond.mcfunction
 with the following /function command:
/function 9Minecraft:give_diamond
As you can see, you do not need to enter the .mcfunction extension when using the /function command.
TIP #1:Â Make sure you are in the correct saved world. Since each function is stored under theÂ
data\functions\
 folder for a specific saved world, the function can only be run from within that saved world.TIP #2: Always run the /reload command after saving or changing a function file before using the /function command.
Examples
Example in Java Edition (PC/Mac)
To run the function 9Minecraft/give_diamond.mcfunction
 with no conditions:
/function 9Minecraft:give_diamond
To run the function 9Minecraft/create_fence.mcfunction
 if there is a cow within 20 blocks:
/function 9Minecraft:create_fence if @e[type=cow,r=20]
To run the function 9Minecraft/chest.mcfunction
 if there are no llamas within 100 blocks:
/function 9Minecraft:chest unless @e[type=llama,r=100]
How to Enter the Command
1. Open the Chat Window
The easiest way to run a command in Minecraft is within the chat window. The game control to open the chat window depends on the version of Minecraft:
- For Java Edition (PC/Mac), press the T key to open the chat window.
- For Pocket Edition (PE), tap on the chat button  at the top of the screen.
- For Xbox One, press the D-Pad (right)Â Â on the controller.
- For PS4, press the D-Pad (right)Â Â on the controller.
- For Nintendo Switch, press the right arrow button  on the controller.
- For Windows 10 Edition, press the T key to open the chat window.
- For Education Edition, press the T key to open the chat window.
2. Type the Command
In this example, we will run the function called give_diamond.mcfunction in Minecraft Java Edition (PC/Mac) 1.20 with the following command:
/function 9Minecraft:give_diamond
Type the command in the chat window. As you are typing, you will see the command appear in the lower left corner of the game window. Press the Enter key to run the command.
Once the cheat has been entered, all commands within the function will be executed.
You will see the message “Executed 1 command(s) from function ‘9Minecraft:give_diamond’” appear in the lower left corner of the game window to indicate that the function was successfully run.
In this tutorial, the file called give_diamond.mcfunction is very simple and only has the following command inside:
give @p diamond
TIP: When entering commands inside your function, do not type theÂ
/
 character at the start of each command.
If you want to execute multiple commands in a function, just enter each new command on a separate line inside the function file.
For example:
give @p diamond give @p gold_block setblock ~ ~ ~1 tnt 0 replace
Congratulations, you just learned how to use the /function command in Minecraft!