# Functions in Hopscotch - abilities with inputs and outputs

A function, at a short explanation, is a bit of code that can be repeated, and take inputs plus produce outputs. This is a more abstract concept but it means we can apply it to a number of concepts/ideas, and can be handy.

In Hopscotch, we have the feature of abilities for bits of repeated code. However, there aren’t default features for inputs or outputs.

I am going to illustrate with a few examples.

## Inputs

Extra background information

If you’ve encountered other programming langauges, you might see that you’re able to set the names of inputs for functions and then use them inside the function.

e.g.

``````function jump(height)
Change Y By (height)
Turn 360 degrees
Change Y By (-1 * height)
end

jump(150)
``````

These are actually just variables that you’ve set and used like other variables, but inside this function.

It would be like doing this if you didn’t have a function:

``````Set height to 150

Change Y By (height)
Turn 360 degrees
Change Y By (-1 * height)

``````
The default Jump ability in Hopscotch is this:

But say I don’t want to make it jump exactly 100 each time and I want to be able to change that easily.

I might put variables in instead, as that will let me change it more easily:

Now if I want to set it to, say, 200, I can do that. If I do it inside the ability though... 🤔

hmmm, so each time I use the ability, it will always be 200… whereas I want to be able to change the variable each time I use the ability.

Setting the value will have to be separate from using it inside the ability, so I’ll put it outside. If I do this each time instead (here’s the main part):

I can do things like this – Jump(200) and Jump(300):

which saves more time for me than writing these:

so now you have a Jump ability that takes in inputs

## Outputs

I can only think of a maths function at the moment… but let’s say you have a formula like Pythagoras’ theorem for calcuting the length of the longest side of a right-angled triangle `√(a² + b²)` (you can choose your own exciting formula).

You have an ability that calculates this for you, along with being able to set the input variables `a` and `b`.

If you store the result in a variable, you can now actually use that result elsewhere too Like an output!

E.g. I could use this output as the length of a line

This is for the general abstract concept (even though I don’t quite hit the point )

Link to the function outputs project.

It’s not as convenient as, say, a reporter block like in Scratch or Snap but it can be done (and anyway, sometimes you wanna code on the go and you don’t have other languages if you know them)

After each time you set an input and call an ablility, you can reset the input straight after … and then you can do something called recursion maybe I will include that for another time For me, that part is thanks to the projects of GameDoctor (or Emily from Hopscotch!)

All of this is free to use, no credit needed.

I love thinking about alternatives for constructs in Hopscotch like this. You see more of a need/purpose/reason for each of these, and it is easy to take things for granted otherwise Although of course having it as a default is handy too!

23 Likes

Awesome topic! I will add to it!
(Why did you use “ü” in your title?)
(I can delete this post if you want it to be organized)

4 Likes

Thanks! The ü was a temporary workaround as there’s a forum bug where it mistakes bad words as being separate in the title (you’ll see if you try to edit it back to a normal u). And wow thanks for offering to edit, I didn’t think there would be anything that people would want to add

And this post is fine, if anything it’s great!

4 Likes

Yeah, I remember when the titles didn’t allow you to use the letter “k” and I had to use a Unicode character k with a circle around it: ⓚ

Changed the title with a “υ”
Let’s see if people notice

4 Likes

Ooh thanks for that!! I would not have picked it out

the title bug affects the links of the topics, but oh well.

4 Likes

Yes. Functions are really amazing! I’ve been coding with them on swift lately.

3 Likes

Really? That’s too bad! I’ll try to find something else that’ll work

Also, we should add a section for buttons, when a certain button is tapped, the variable is set to a certain number to increase the height of a jump

Or if a certain “y pos” is tapped on the screen, the character jumps that high divided by a certain number

3 Likes

That’s okay, don’t worry the link problem wasn’t due to the letter! it would be that way if it were any other character.

And ooohh I see, I was actually using the Jump ability as an example for functions in general (which could be applied to drawing a line of variable length as another example) but I love how you have those ideas too! Hmm, I am thinking that it might be easier if you continue with another topic (I don’t mind in the least if you copy and paste things over)

this is an example of the feature that I was emulating here for this topic:

from (Snap)

4 Likes

One question @t1_hopscotch,
How much have I missed of all of the cool stuff on Hopscotch!?!??!?!

2 Likes

Ok, I see, that would make sense
There’s a “Teaching Code” Category, right? If not, I’ll put it on the list of my Help Invent Categories topic

3 Likes

Hmm, that would be cool if there was a category for that! I guess they included things like drawing pads under the description for this category, so maybe just code for projects in general could be put here

Hi @Phase_Admin i haven’t been around much so I don’t really know what I would have missed either

4 Likes

lol
I have been gone some time though.

1 Like

Yeah, I’ll be making a lot of Code Teaching projects!

4 Likes

That is awesome!!

3 Likes

Thanks!
(I think a How To category would be really nice)

3 Likes

Wow, this topic is great! I love the logic behind it! I’ve never thought of using anything like this before. :D

4 Likes

Wow that’s really cool!! I think I’ve done something like this before without realizing it XD Using abilities with inputs/outputs can be very useful, though Great tutorial. It’s really easy to understand

4 Likes

Thanks I am doing a class where we look at things more defining some abstract things and another way of framing our thinking (it is in relation to algorithms and problem solving), i hadn’t thought about much in that way before it!

5 Likes

Awesome tutorial! This is a great way to replicate functions in Hopscotch, although it still would be great if such a feature was built-in

4 Likes

I just thought of a more useful example for the output part

4 Likes