Functions in Hopscotch - abilities with inputs and outputs

alternatives
functions

#1

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 :confused: 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 :smiley:

This project is available here.


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 :smiley: 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 :joy:)

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 :blush: (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 :open_mouth: maybe I will include that for another time :slight_smile: 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 :slight_smile: Although of course having it as a default is handy too!


Factorial fυnction and combinations fυnction
Code Help Please!
Hopscotch, Scratch, and other coding engines
Hopscotch Programming Puzzles
Introduction to fractals and recursion
Recursion in Hopscotch and generally
Topics with censored heading bug
#2

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)


#3

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 :joy:

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


#4

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


#5

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

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


#6

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


#7

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


#8

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)


#9

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


#10

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


#11

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 :open_mouth:


Hi @Phase_Admin :smiley: i haven't been around much so I don't really know what I would have missed either :joy:


#12

lol
I have been gone some time though.


#13

Yeah, I'll be making a lot of Code Teaching projects! :slight_smile:


#14

That is awesome!! :smile:


#15

I learned about functions this month. Hehe. I found out I have an app that has them. They're really useful.


#16

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


#17

I put all my teaching code stuff in the #open-source-code category.


#18

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


#19

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


#20

Thanks :smile: 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!