Saturday, December 11, 2010

What are functions good for?

At this point, we've seen one reason why functions are useful. Functions let us create logical groupings of code. If someone is reading your code, and she sees that you call a function bakeCookies, she knows immediately that you are baking cookies. If, on the other hand, she sees that your code places eggs in a bowl, then adds butter, etc., it will not be clear right away that you are trying to bake cookies. Lots of recipes start out with putting eggs in a bowl, and lots of recipes add butter to the eggs. By the time she reads the last line, she might realize that you are baking cookies, but only if she is familiar with the recipe. It's possible that she won't realize that you are baking cookies at all! The point is, functions make your code much easier to read.
There is an even better reason to use functions: they can make your code shorter. Fewer lines of code is not always desirable, but every time you write a line of code, there's the possibility that you are introducing a bug. Functions start to reduce the number of lines of code when you call them repeatedly.
Suppose that you want to mail out invitations to eight of your friends for a cocktail party. Let's assume that you need to do the following procedure in order to invite your friend Hank.
write Hank's name on the invitation
  write Hank's name and address on the envelope
  place the invitation in the envelope
  seal and stamp the envelope
  drop the envelope in the mail
It takes five lines of pseudo-code to invite one friend, so it takes 40 lines of pseudo-code to invite eight friends. It would look like this:
write Hank's name on the invitation
  write Hank's name and address on the envelope
  place the invitation in the envelope
  seal and stamp the envelope
  drop the envelope in the mail

  write Ann's name on the invitation
  write Ann's name and address on the envelope
  place the invitation in the envelope
  seal and stamp the envelope
  drop the envelope in the mail

  write Alicia's name on the invitation
  write Alicia's name and address on the envelope
  place the invitation in the envelope
  seal and stamp the envelope
  drop the envelope in the mail

  write Whitney's name on the invitation
  write Whitney's name and address on the envelope
  place the invitation in the envelope
  seal and stamp the envelope
  drop the envelope in the mail

  write Greg's name on the invitation
  write Greg's name and address on the envelope
  place the invitation in the envelope
  seal and stamp the envelope
  drop the envelope in the mail

  write Mi Young's name on the invitation
  write Mi Young's name and address on the envelope
  place the invitation in the envelope
  seal and stamp the envelope
  drop the envelope in the mail

  write Flavio's name on the invitation
  write Flavio's name and address on the envelope
  place the invitation on the envelope
  seal and stamp the envelope
  drop the envelope in the mail

  write Brian's name on the invitation
  write Brian's name and address on the envelope
  place the invitation in the envelope
  seal and stamp the envelope
  drop the envelope in the mail
That's a lot of repeated code, and any time you repeat code like this, you are more likely to add a bug to your program. For example, look at what we're doing with Flavio's invitation - we are placing it on, not in, the envelope! We sealed his envelope and dropped it in the mail, but there was no invitation inside. Flavio will receive an empty envelope and he'll be mighty confused. That's a mistake that resulted from having to type the same lines of code over and over again.
Functions can substantially reduce the amount of pseudo-code you need to write to invite your eight friends to the party. It seems unlikely that you'd be able to reduce this at all - each of your friends has got to have their own personally addressed invitation, and all of the envelopes have to be sealed and stamped and placed in the mail. How are we going to reduce the number of lines of code? Let's create a function called inviteToPartywhich does the following procedure:
write Hank's name on the invitation
  write Hank's name and address on the envelope
  place the invitation in the envelope
  seal and stamp the envelope
  drop the envelope in the mail
Now that we have this function, we can call it eight times to invite our eight friends:
inviteToParty
  inviteToParty
  inviteToParty
  inviteToParty
  inviteToParty
  inviteToParty
  inviteToParty
  inviteToParty
You probably noticed a problem with doing it this way. We're inviting Hank eight times, and none of our other friends are going to receive invitations! Hank will get invited eight times because the function invites Hank to the party, and the function is being called eight times. The solution is to modify the function so that it invites friend to the party, where friend can be any of your friends. We'll change our function so that it looks like this:
write friend's name on the invitation
  write friend's name and address on the envelope
  place the invitation in the envelope
  seal and stamp the envelope
  drop the envelope in the mail
and then we'll change the way in which we call the function:
inviteToParty (friend = Hank)
  inviteToParty (friend = Ann)
  inviteToParty (friend = Alicia)
  inviteToParty (friend = Whitney)
  inviteToParty (friend = Greg)
  inviteToParty (friend = Mi Young)
  inviteToParty (friend = Flavio)
  inviteToParty (friend = Brian)
Now, each time we call the function, friend is a different person, and each of our eight friends will be invited. We've just reduced the number of lines of pseudo-code from 40 to 13 by using a function, and our code became much easier to read. (We also got rid of that bug whereby Flavio received an empty envelope.)
All of the examples on this page were written in pseudo-code, but the next page describes how to write functions in C++.

No comments:

Post a Comment