MediumUnderstanding Modular Functions. Have you ever started working on a game… | by Kiki Prottsman | Kiki’s Corner | Medium
Sitemap
Kiki’s Corner

Whether you’re a student or a teacher, a beginner or expert, drop by weekly to find new tips, suggestions, and challenges!

Understanding Modular Functions

5 min readJan 6, 2025

--

Have you ever started working on a game in Microsoft MakeCode Arcade and watched it get larger and larger…and also messier and messier? I have a simple way to prevent that, and it all has to do with modular functions!

What Are Modular Functions?

Modular functions are chunks of code that are completely self-contained. They have a specific input and a specific output and because of that, nothing else in your program needs to get tied up in *how* that output is accomplished.

It’s like sending a text to a reliable friend saying, “Hey, can you grab cookies?” You’re providing them with an input (the text) and they’re providing you with an output (the cookies) and you don’t need to know what store they went to, how they got there, or what method they used to pay.

In MakeCode Arcade, Modular functions do the same thing for your code. They’re reusable blocks of instructions that you can call whenever you need them. Whether it’s making your character jump, spawning enemies, or counting your high score, modular functions help keep your game code neat and manageable.

Why Bother with Modular Functions?

Good question! Why not just pile all your code into one place and hope for the best? Well, here are some reasons:

  1. Stay Organized: Without functions, your game code can turn into a spaghetti mess of blocks. Modular functions act like labeled bins — tidy, organized, and easy to find when you need them.
  2. Save Time: If you need your game character to do a backflip 10 times at different points throughout your game, you don’t want to scatter those instructions 10 times throughout your code. With modular functions, you create the backflip once and call it anytime using a single block.
  3. Debug Like a Boss: When something’s not working (because…bugs happen) modular functions make it easier to track down and fix the problem. Instead of combing through a jungle of blocks, you can just check the function.
  4. Future-Proof Your Game: What if you decide later that backflips need to include a glitter explosion? Update the function, and BOOM — glitter is added to all of the backflips.

How to Create and Use Modular Functions in MakeCode Arcade

Let’s get hands-on! Open MakeCode Arcade, and we’ll walk through building a modular function.

We are going to make a monkey that teleports and spits when you press the space bar; flips & sparkles when you press the enter key; and teleports, spits, flips and sparkles when you press the up arrow.

Prep the game

Start by adding your monkey, and adding three event containers for on button A press , on button B press , and on button up arrow press . Rename your sprite something meaningful, like “monkey”.

1) Plan Your Functions

First, you need to be very clear about what you want to provide to your function, and what you want to receive from your function. In this program, these are our expectations:

Function 1 - Teleport & Spit
Provide
: Sprite to act upon
Receive: Make provided sprite teleport to a random location and spit for 500ms.

Get Kiki Prottsman’s stories in your inbox

Join Medium for free to get updates from this writer.

Function 2— Flip & Sparkle
Provide
: Sprite to act upon
Receive: Make provided sprite flip to face the opposite direction and sparkle for 500ms.

2) Create Your Functions

Now that you know you need two functions to make these actions happen, you can create the function definition containers (which will also create the blocks to call each function.)

  • Open the “Advanced” category in the toolbox.
  • Select “Functions” and click on + Make a Function.
  • Name your function something descriptive, like spittingTeleport or random_move_and_spit.
  • Click the “sprite” tab at the top to add a sprite parameter. Then click “Done”.
Press enter or click to view image in full size
  • Do the same to create a second function that makes the monkey flip and sparkle.
Press enter or click to view image in full size

3) Add Blocks to Your Functions

Your new function will look like an empty container. Drag in the blocks you need to make the sprite do what you’re asking.

Drag the red “mySprite” parameter into any block that affects your sprite. DON’T BE FOOLED by the “mySprite” variable that’s already in the block. It’s a different mySprite and it will give you an error if you leave it.

Press enter or click to view image in full size

3. Call the Function

Now that you’ve built your function, it’s time to put it to work! Wherever you want the function to run, drag in a call [function name] block. Don’t forget to change the name of the sprite in each function call from “mySprite” to whatever you named your monkey. From here, it’s just like using regular blocks, but you made them yourself!

Press enter or click to view image in full size

In this case, we want to run teleportSpit when A is pressed, flipSparkle when B is pressed, and both teleportSpit AND flipSparkle when the up arrow is pressed.

4. Test and Refine

Hit the play button and test your game. If something doesn’t work as planned, tweak your function until it behaves just right.

Can you see how utilizing this method could help keep your code nice and tidy, even as your program gets really big?

Press enter or click to view image in full size
The finished game code with functions collapsed to keep the workspace tidy

Can you also see how it might help multiple people program together? If one person writes the main program and assigns others their own modular functions, all you have to do is combine them in the end and everything should just work!

Drop a comment below with your favorite ways to use functions in MakeCode Arcade — or let me know what you’re building! I’d love to hear about it.

Until next time, happy coding

--

--

Kiki Prottsman
Kiki Prottsman

Written by Kiki Prottsman

Kiki is an author, educator, and the Director of Education for Microsoft MakeCode

Kiki’s Corner
Kiki’s Corner

Published in Kiki’s Corner

Whether you’re a student or a teacher, a beginner or expert, drop by weekly to find new tips, suggestions, and challenges!