Methods with Parameters
Connection to earlier learning
Earlier this year, we learned how to use functions.
Functions are an example of applying abstraction, which generally helps us to avoid writing repetitive code.
For example, at the start the lesson to reproduce the Replacements gig poster from last module, we saw how a simple function, written in Swift, allows us to save many lines of code:
Later, we learned how adding parameters to our functions make them even more useful. This process is referred to as generalization.
We used a function to draw the "circle in square" pattern:
The function draws the same design each time, differentiated only by the parameters that exist – in this case – a parameter for the color – and a second parameter for the anchor position to draw from:
We are now going to apply this same idea in a 3D Alice world.
Terminology
First, though, a brief sidebar about terminology.
Software developers use the following terms nearly interchangeably:
- function
- procedure
- method
You will read all three of those words in this lesson.
When you read them, think: "This is a way to teach the computer a new trick."
Essentially, those three terms mean the same thing.
Eliminating repetitive code
After completing the "Lost and Found" exercise in the prior tutorial, you would be forgiven for feeling it was a little tedious – there is so much duplicated code!
Surely there is a better way!
Of course there is.
We can apply abstraction so that what currently takes thirty-five lines of code takes just seven (not including comments) instead:
Just as we "taught" the turtle new tricks in prior modules of this course, we can "teach" all objects of a certain type how to approach an item.
Inheritance and data types
Before we do so, a brief digression to discuss data types is required.
Alice is an object-oriented programming environment.
When you drag a tile from the gallery into an Alice world, you are creating an instance of a class:
Here an instance of the AdultPerson
class is being created.
The instance, also called an object, is named astronaut
.
Classes exist in a hierarchy:
Crucially, a class will inherit capabilities from its parent classes.
What does that really mean?
If you teach the Biped
class to do something new, then all subclasses of Biped
will inherit that same capability.
Creating a procedure on a class
We need to make it possible for our ElderPerson
instance in this world to look for their AirPods.
If desired, you can download this Alice world to follow along with the tutorial from this point forward.
To do this, we will create a procedure named approach
on the Biped
class.
Create the procedure
In order, we must:
- Tap the class navigator button.
- Select
Biped
. - Select
Add Biped Procedure
.
Now we give the new procedure a name – by convention, we use camelCaseNaming
:
Add code to the procedure
Switch back to myFirstMethod
by clicking it's tab (1):
Hold the Option
or ⌥
key down and drag the tile that contains the code you want to use into the clipboard:
Now switch back to the approach
method (1):
And drag the code out of the clipboard:
Generalize the procedure
At this point you may be concerned because there are a lot of red error tiles:
These red error tiles exist because they refer to specific objects within our world.
This is a problem, because when we teach a class – Biped
in this case – how to do something new – the procedure must work in all situations – not just with specific objects in a specific world.
So, we must generalize – this is a key part of applying abstraction.
Fortunately, it's a relatively simple process:
- Where we see
this.elderPerson
we'll instead saythis
which refers to whatever object will do the work of approaching something in the future. - Where we see
this.bell
we'll instead add a parameter nameditem
. So theapproach
method can be used to approach any item – not just a bell. - Finally, where we see
this.origin
we'll instead add a parameter namedreturningToStartingPoint
. So theapproach
method can be used to have the biped return to a provided starting point after it approaches an item.
So, let's do this in order.
First, replace all instances of this.elderPerson
with this
:
Second, we add a parameter named item
. It's value type will be Prop
. This controls what type of objects can be passed into the parameter:
Now, whereever we see this.bell
we replace the reference with the parameter named item
:
Third and finally, we add a parameter named returningToStartingPoint
to allow the user of the procedure to specify where the Biped returns to after approaching an item:
Then, whereever we see this.origin
we replace the reference with the parameter returningToStartingPoint
:
Use the procedure
Now we can go ahead and use our new procedure!
This is fun because we get to delete a lot of repetitive code and use the procedure instead.
For the first object – the bell – here's how to replace the old code with the new procedure:
Exercises
Exercise 1: Replace repetitive code
For the remaining objects in this scenario, replace the old code with the procedure instead.
When you are done, the program should look like this:
Exercise 2: Three Little Pigs
Create a scene for the children's story known as The Three Little Pigs.
This will involve a wolf approaching three different houses, and saying something to each pig.
Consider how you could make use of a procedure in this scenario:
- What actions would always be the same?
- What actions would differ from one pig to the next?
- That is where parameters are needed.
Alternatively, create a different, original scene that makes use, in some way, of a procedure that has parameters.