What is a Function? |
Up until this point, every line of code we've shown you has done a simple
task, such as performing an arithmetic operation, or checking a boolean
condition, or assigning to a variable.
Functions
allow you to do a whole lot in one line of code. Instead of performing a simple
task, a single line of code can display a menu of choices, or compute
complicated three-dimensional transformations, or even play Tetris!
How is this possible?
Functions allow you to group a series of steps under one name.
In C/C++, and in most programming languages, you can give a name to a series of
steps. Let's say we want to call this procedure "calculate average". Then, our
algorithm for calculating the average becomes:
calculate average
The steps of calculating the averages of 2 numbers would be as follow:
Get number1
Get number2
Add the 2 numbers
The average would be the summation divided by 2
In this example, what you would do is write a function
called CalcAvg (C++ won't let you put spaces in the names of
functions or variables) that performs the series of steps above, and then
whenever you wanted to calculate an average , you would
call the function
CalcAvg, which would execute the lines of code necessary to carry out
the procedure.
Note: When we say you are calling the function
CalcAvg, we do not mean that you are giving it the name
CalcAvg- you've already done that by writing the function. We mean
you are executing the code in the function
CalcAvg.
"Calling a function" really means "telling a function to execute".
At this point, we've seen one reason why functions are useful. Functions let us create logical groupings of code.
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 5 of your friends for a party. Let's assume that you need to do the following procedure in order to invite your friend Waleed.
write Waleed's name on the invitation write Waleed'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 25 lines of pseudo-code to invite eight friends. It would look like this:
write Waleed's name on the invitation write Waleed'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 Esmaeel's name on the invitation write Esmaeel'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 Tamer's name on the invitation write Tamer'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 Yaser's name on the invitation write Yaser'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 Khaleel's name on the invitation write Khaleel's name and address on the envelope place the invitation on 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 Khaleel'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. Khaleel 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 5 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 inviteToParty which does
the following procedure:
write Waleed's name on the invitation
write Waleed'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 5 friends:
inviteToParty inviteToParty inviteToParty inviteToParty inviteToParty
You probably noticed a problem with doing it this way. We're inviting Waleed 5 times, and none of our other friends are going to receive invitations! Waleed will get invited 5 times because the function invites Waleed to the party, and the function is being called 5 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 = Waleed) inviteToParty (friend = Esmaeel's ) inviteToParty (friend = Tamer ) inviteToParty (friend = Yaser ) inviteToParty (friend = Khaleel )
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 25 to 13 by using a function, and our code
became much easier to read. (We also got rid of that bug whereby Khaleel 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++.
Summary |
Think of a function as a black box.
A "black box" is a convenient analogy for something that happens by magic. How does the black box work? It takes some inputs, and swirls them around inside the box, and produces some kind of output. Each function can swirl around the inputs in a different way, however that function so chooses. How a function will use the inputs to come up with the outputs is the essence of the function.
We just said that functions are like black boxes because we throw in some inputs, and something happens by magic, and some output comes flying out. The black box metaphor is really only appropriate when you are using other people's functions. When you write the function yourself, you have to know exactly how the function swirls up the inputs. It's not magical for you, because you decided how the function works. However, if you give your function to someone else for them to use, you can tell them it's a black box.