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.
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.
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)
But say I don’t want to make it jump exactly 100 each time and I want to be able to change that easily.
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):
so now you have a Jump ability that takes in inputs
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
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!