Looking back at the calculation to get the Angel and Coach comfortably close, we can observe a number of things:
It's a big calculation.
It was fiddly to create, and it is quite easy to make an error while assembling such a calculation. We don't want to create such calculations often, if we can avoid it.
If we didn't know what the calculation did, it would be tricky to look at it and work it out.
These observations are related to the reasons for creating methods - we may have a big bit of code that we may want to reuse without having to reconstruct it every time and we may want to hide, or abstract, some of the complexity of the code.
Methods allow us to hide and reuse collections of instructions. Clearly, we want to do the same thing with a complex calculation like this. That is exactly what a function is - something that hides the detail of calculating a value and is reusable in many places.
Methods and functions have a lot in common. They have a name, and they can optionally take parameters. They contain a body of code.
The big difference with a function is that it returns a value. Just as executing a calculation gives back a value, so does executing a function, which is after all just a kind of wrapper around a computation. Think about your use of the purple function tiles so far - you use them to get a value that you need - for example, the distance to, or width of, something. You call the function, provide whatever parameters it needs, and it, internally, does some computation and gives you back the result of that computation, which is then incorporated into your executing code.
With this in mind, let us explore how to create our own functions. We'll create a function that works out the distance required to move one actor so that it is comfortably close to another actor.
Select world in the top left pane, and then make sure the functions tab is selected in the lower left pane.
Click on 'create new function', typing in comfortable distance as the name, and ensuring 'Number' is selected as the type. Setting the type to Number means that the function will return a numerical value.
3. Create two parameters for the new function, called Actor1 and Actor2, both of which should be of type Object.
You'll see in the body of the function that there's already a Return tile in place. All functions must return a value, and the default computation here is simply the value 1. We need to change this computation to be the one we want. Note that this is a complicated process in Alice!
4. First we need to get the distance to function of the parameter Actor1. We can't do this directly by dragging Actor1 over the value 1 unfortunately, so we need a bit of trickery.
5. Instead, drag in the distance to function of the Angel and drop it on the value 1.
6. When you drop it, Alice will ask you for the object you want the distance to. We want the distance to Actor2, and you will find this under the expressions menu item.
7. Now you can click on angel in the distance to function call you've created, and set that to Actor1 - again found in the expressions menu entry.
8. Extend the calculation as you did before, by adding "- 1" on the end.
9. The "1" needs to become Actor1's width, and the only way to do this is to drag across the width method of another object, for example the angel. So drag that across now and drop it onto the 1.
10. As before, you can click on the object name of the width function you just dragged in, and via the expressions entry, change this to Actor1.
11. Now divide the result of the width function call by 2, as you did earlier.
12. Repeat steps 3-6, only now you are aiming to create "- (Actor2's width / 2)".
Finally, add on the final "-0.5".
Now you are in a position to use this function just like any other. Try it out! You could replace the calculation in your code to move the Angel to the Coach, or you could change the code in AngelGoToEgyptian. When you want to use the function, simply drag it from the bottom left panel (assuming World is selected and the functions tab), and supply values for the two parameters. In this case switch Actor1 to the Angel and Actor2 to the Coach.
Make sure to save the World you created on this Page.
Recommended File Name is M7-pg6-Function-YourName.a2w
[Q7.6.1]: Does it matter whether or the Angel and Coach are Actor1 or Actor2? Try reversing these parameters and see what happens. Can you explain the results?