# How do functions work?

**URL:** https://forum.makecode.com/t/how-do-functions-work/28703
**Category:** Help
**Created:** [May 10, 2024, 8:51pm UTC](https://forum.makecode.com/t/how-do-functions-work/28703 "2024-05-10T20:51:06Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Brainstorm\_Studios](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/brainstorm_studios/32/20476_2.png) [@Brainstorm\_Studios](https://forum.makecode.com/u/Brainstorm_Studios)
#### Post date: [May 10, 2024, 8:51pm UTC](https://forum.makecode.com/t/how-do-functions-work/28703/1 "2024-05-10T20:51:06Z")

</div>

i only really know the bare minimum about functions, calling lines of code without any other special things. i know there are more things i can add like sprites, variables, and booleans, but how?

---

<div class="post-metadata">

### Author: ![Taser](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/taser/32/29517_2.png) [@Taser](https://forum.makecode.com/u/Taser)
#### Post date: [May 11, 2024, 2:10am UTC](https://forum.makecode.com/t/how-do-functions-work/28703/2 "2024-05-11T02:10:20Z")

</div>

As far as I’m concerned, you can simply create/initialize variables (any type of them, whether they’re boolean, integer, or decimal) inside a function; they are called local variables. And, as the name implies, they’re **local** , they can only be accessed through the function.

![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/f/fee0ba031804c339b7cec09d6322a93a9dc8d702.png)

Sure, the program won’t break down if you decide to use the local variable outside of the function, however, in reality, you just made 2 variables of the same name.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/9/947f5c6e1e9730a2e1ec3acf8fa95c095f7d0191.png)  
Button Local Variables  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/d/dd541b6bfae6c751ad919ababec8964d59961ec8.png)

If you want a function to spawn a sprite, I’d use a local variable (variables that can only be accessed in a specific function), and assign a sprite for the variable, for example: if I wanted to spawn a button at a random location every time I pressed spacebar, I would’ve wrote this function down here.  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/6/6f05794f88600f9127bae0e8e78488791e5ac0ea.png)

Here’s the code:

> **[Donut Spawn Stuff](https://arcade.makecode.com/34343-32528-18132-96863)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

---

<div class="post-metadata">

### Author: ![robotosaurus](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/robotosaurus/32/19294_2.png) [@robotosaurus](https://forum.makecode.com/u/robotosaurus)
#### Post date: [May 11, 2024, 2:10am UTC](https://forum.makecode.com/t/how-do-functions-work/28703/3 "2024-05-11T02:10:22Z")

</div>

Hmm without much context I can’t help you.

---

<div class="post-metadata">

### Author: ![Luke](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/luke/32/32519_2.png) [@Luke](https://forum.makecode.com/u/Luke)
#### Post date: [May 11, 2024, 2:10am UTC](https://forum.makecode.com/t/how-do-functions-work/28703/4 "2024-05-11T02:10:34Z")

</div>

You can essentially make a custom block  
Example: I’m trying to add custom emotion animations  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/b/b616946fd20f43b873f684e543aa23a1c838f006.png)

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/5/5731ce46628403bb42378f19d04b1ea871e35987.png)  
Now the text inputted in the function is sent to the bottom line  
this just stores it on the sprite  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/0/0ae4e629e6753ea6b6cac3d1c1a28e53fe0c85f8.png)  
Finally, the data is read in this if statement and the sprite animates  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/7/72c62e4087fe269ac718f255a979a6f445231ded.png)  
This is useful for repeating the same action - displaying an emoji on a sprite  
but also making it able to be used for multiple different emoji types

Example 2: moving a sprite

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/0/09286ab3a10ce6af504f67483322395293988d69.png)  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/5/5a0b793a3bce86888cbca1a631f951d85c50a6b9.png)

Can edit the number of times the sprite moves, the amount it moves by, and the pause between each move repeat (can add even more if you want to customise further)  
This is useful for making custom movement with only 1 function (less blocks)  
also makes it easier to edit the code as changes affect all 4

---

<div class="post-metadata">

### Author: ![ARandomGuyOnMakeCode](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/arandomguyonmakecode/32/22983_2.png) [@ARandomGuyOnMakeCode](https://forum.makecode.com/u/ARandomGuyOnMakeCode)
#### Post date: [May 11, 2024, 2:10am UTC](https://forum.makecode.com/t/how-do-functions-work/28703/5 "2024-05-11T02:10:41Z")

</div>

> [@Brainstorm\_Studios](#):
>
> i know there are more things i can add like sprites, variables, and booleans, but how?

By add more things, do you mean adding parameters?

---

<div class="post-metadata">

### Author: ![Josef](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/josef/32/12457_2.png) [@Josef](https://forum.makecode.com/u/Josef)
#### Post date: [May 11, 2024, 2:12am UTC](https://forum.makecode.com/t/how-do-functions-work/28703/6 "2024-05-11T02:12:02Z")

</div>

Look at the code from The Makecode Forums. It’s pretty self explanatory

---

<div class="post-metadata">

### Author: ![WoofWoof](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/woofwoof/32/32523_2.png) [@WoofWoof](https://forum.makecode.com/u/WoofWoof)
#### Post date: [May 11, 2024, 2:15am UTC](https://forum.makecode.com/t/how-do-functions-work/28703/7 "2024-05-11T02:15:59Z")

</div>

Just…look at this:

 ![IMG_3619](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/d/d3eeffca9d0069f3b519736d6072ee7153108d60.jpeg)  
This is very simple, but so, so much more can be done.  
[Here is the project](https://arcade.makecode.com/S64413-05134-46566-47292) if you want to change it to a different language or mess around with it 🙂

---

<div class="post-metadata">

### Author: ![UnsignedArduino](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/unsignedarduino/32/592_2.png) [@UnsignedArduino](https://forum.makecode.com/u/UnsignedArduino)
#### Post date: [May 12, 2024, 1:00am UTC](https://forum.makecode.com/t/how-do-functions-work/28703/8 "2024-05-12T01:00:45Z")

</div>

MakeCode doesn’t support local variables in blocks, everything is global. 😭😭😭

So if you used a recursive function for example, it would definitely break.

---

<div class="post-metadata">

### Author: ![Brainstorm\_Studios](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/brainstorm_studios/32/20476_2.png) [@Brainstorm\_Studios](https://forum.makecode.com/u/Brainstorm_Studios)
#### Post date: [May 13, 2024, 2:05am UTC](https://forum.makecode.com/t/how-do-functions-work/28703/9 "2024-05-13T02:05:06Z")

</div>

Yeah.
