I was reading through the 2nd edition of HFJ just now and I thought the example given of the vending machine was pretty good. I'm not sure if you have the 1st edition though and how different that is from the 2nd edition because I couldn't find anything that mentioned Monsters or any of the other creatures you have. I thought the explanation that I read about return in HFJ was pretty straightforward though so let me use the same vending machine analogy.
Pressing buttons on a vending machine to choose an item you want to buy is like passing in a parameter to a method of a vendingMachine object (let's ignore the fact that you have to put in money, too, for now).
You're basically saying, "Hey vending machine, give me one of those items that's marked J2".
In code, that might look something like this:
See how it can translate into a conversation or a description of what is happening in your program? The
vendingMachine is the object, the
giveMe() method is a command that the object responds to, or you could also look at is as a behavior or capability the object has, and
"J2" is a parameter that is a detail that's relevant to your specific request for the object to do something. That detail can be different each time you make a request and the object will adjust accordingly. Like if you say "D1" instead, you'll get back a different item, the one that's marked "D1".
When the vendingMachine gives you back something based on your request and the detail that you gave it, that's ultimately because of a
return statement. The vendingMachine made some kind of determination that when you asked it to give you a "J2" item, it needed to spit out a candy bar, for example. It would use the same logic to make a determination that it needed to spit out some chips if you had asked it to give you a "D1" item instead. In code, that determination might have looked something like this:
For now, don't pay attention to the fact that this example shows a very naive implementation, I'm just trying to make it as simple as possible so you can relate to what's happening. The point is that there is code that somehow figures out which particular item needs to be spit out (returned) based on the specific information you passed in when you called the
giveMe method. If you're wondering what those ellipses are (the ... things), don't worry about that stuff for now. That just means that the
giveMe() method probably does a few other things to make sure it's doing the right thing. That's not relevant to this discussion so ignore it for now.
Now, back in the outside world, the
bought variable will
receive the value that the
giveMe method returned, specifically, the "candy bar" Item. In that way, the
bought variable essentially
becomes the Item that was returned by the vendingMachine when you asked it to give you the item marked "J2". You can then go off and enjoy the Item (a candy bar) that you just bought.
Are you with me so far?
Now, you could have also just written this:
This time, you didn't assign the returned value to anything. That means that the Item that the vendingMachine returned wasn't
received by anything. It's just sitting there. It's like you pressed the button and just walked away without taking your candy bar with you. I'll do that sometimes, especially when I see that the electronics department has some new items on display. I'm old and absentminded, what can I say.
What happens to the Item that the vendingMachine returned, the one that you just left there?
Well, there's a guy walking around the store and he's waiting to pick up garbage that people leave laying around, things that they don't use anymore. This guy is called, very appropriately, the "Garbage Collector". When you walk away from an Item like that, sooner or later, and you don't really know or have to care when because he tries to stay as inconspicuous as possible, the Garbage Collector guy will come around and notice that there's an Item (oooh, a candy bar!) that's just sitting there unclaimed by anybody. He'll look around the store to make sure that nobody has a sign that says "That's my candy bar!" and then picks it up and throws it out. Yeah, I know, I hate putting a good candy bar to waste but that's how the Garbage Collector guy keeps the store free from clutter.
So you see, you can treat your program like it's just one big story about what's happening between the people using the program and what's happening inside the computer.
Context Matters when Choosing Names
Notice how I picked names that make sense in the context that they're used?
On the outside of the vendingMachine, I used the variable name
bought, as in "the Item that the customer
bought" -- yes, I know, we didn't literally buy anything because no money changed hands but you get what I mean, right?
Inside the vendingMachine, I used the name
selected, as in "the item that the customer
selected". That's because from the vendingMachine's point of view, that's what the item represents. I use a name that properly represents the thing that it refers to in the context that the name is being used.
It's like if you were with your father and I was introducing you to some other people, I would say something like, "This is Sander, and he is the
son of Mr Koorevaar." Now, if you were with your uncle, I would introduce you differently, right? I might say something like "This is Sander, he is the
nephew of Mr Koorevaar". I wouldn't introduce you as your uncle's son, right? The label "son" would not be the right term to use in that situation. This what is meant by "semantics" and "context". Semantics refers to what the
word means and Context refers to the situation in which that particular meaning applies. You always want a name/label to convey a meaning that fits the situation. In the example, the same person/thing (You, Sander) will be given different labels/names (son/nephew) depending on the context/situation (relation to father/uncle).
It's important to choose semantically correct names that fit the context in which they are used. This makes your program make sense when someone reads it. In other words, it clarifies the intent of your program. It's important to take care and spell things out and be clear about what you're saying, otherwise, other people reading your program will be confused as to what's going on. But maybe I've said too much already with regard to your original question so I'll save that stuff for another post and another example.
Does this help?
(EDIT: looking back at the HFJ 2nd Ed again, it appears I misinterpreted the picture that I saw earlier this morning. In the section called
You can get things back from a method, there's a picture of a lady standing in front of what is appears now to be an ATM, not a vending machine. My mistake. Ah well, what's done is done, and I think the vending machine works just as well anyway)