Java Reference
In-Depth Information
•Sugar
•Milk
•Eggs
•Vanilla
•Cocoa powder
•Confectioner's sugar
•Butter
•Milk
•Mix and spread on cake
Which part of the list is for the cake itself, and which is for the frosting? Maybe
the frosting part starts at the cocoa powder. Then again, maybe it's a chocolate
cake base with a vanilla frosting. The point is, it's hard to tell. It might work
as is, but if you need to figure out what's going wrong it will be very hard.
And if you need to make any changes, it will be harder still. Suppose you
have some strange relatives who want their cake to have an orange-apricot
glaze instead of chocolate frosting (I did mention they were strange). Where
do you go in and make the changes?
Instead of one big list, suppose we had broken it up into two steps like this,
where each one lists the ingredients and steps for just that part of the cake-
making process:
makeChocolateCake
makeVanillaFrosting
Oh, now it's easy to see. If there's a problem with the cake, you know where
to look. If you want to do a different icing, you can easily change it to this:
makeChocolateCake
makeOrangeApricotGlaze
That's pretty much the idea behind functions. They are a way to gather
instructions and data together into groups that make sense. But functions
have an extra fun ability: you can use the same function (list of instructions)
with slightly different data. For example, you could have one function named
makeFrosting and call it with different flavorings:
makeFrosting(flavor)
sugar
butter
mix in "flavor"
spread on cake
Then you could use that function, passing in slightly different data as needed:
 
 
Search WWH ::




Custom Search