Chapter 6 Chapter 6 Defining Functions
16.21 Functions, Informally
You can think of a function as a subprogram-a small program inside a program.
The basic idea of a function is that we write a sequence of statements and give that sequence a name. The instructions can then be executed at any point in the program by referring to the function name.
The part of the program that creates a function is called a function definition.
When a function is subsequently used in a program, we say that the definition is called or invoked. A single function definition may be called at many different points of a program.
Let's take a concrete example. Suppose you want to write a program that prints out the lyrics to the "Happy Birthday'' song. The standard lyrics look like this:
Happy birthday to you!
Happy birthday to you!
Happy birthday, dear <insert-name>.
Happy birthday to you!
We're going to play with this example in the interactive Python environment.
You might want to fire up Python and try some of this out yourself.
A simple approach to this problem is to use four
printstatements. Here's an interactive session that creates a program for singing "Happy Birthday'' to Fred.
>>> def main():
print("Happy birthday to you!")
177
print("Happy birthday to you!")
print("Happy birthday, dear Fred. ") print("Happy birthday to you!")
We can then run this program to get our lyrics:
>>> main()
Happy birthday to you!
Happy birthday to you!
Happy birthday, dear Fred.
Happy birthday to you!
Obviously, there is some duplicated code in this program. For such a simple program, that's not a big deal, but even here it's a bit annoying to keep retyping the same line. Let's introduce a function that prints the lyrics of the first, second, and fourth lines.
>>> def happy():
print("Happy birthday to you!")
We have defined a new function called
happy.Here is an example of what it does:
>>> happy()
Happy birthday to you!
Invoking the
happycommand causes Python to print a line of the song.
Now we can redo the verse for Fred using
happy.Let's call our new version
singFred.
>>> def singFred():
happy() happy()
print("Happy birthday, dear Fred. ") happy()
This version required much less typing, thanks to the
happycommand. Let's try printing the lyrics for Fred just to make sure it works.
>>> singFred()
Happy birthday to you!
Happy birthday to you!
Happy birthday, dear Fred.
Happy birthday to you!
6.2. Functions, Informally
So far, so good. Now suppose that it's also Lucy's birthday, and we want to sing a verse for Fred followed by a verse for Lucy. We've already got the verse for Fred; we can prepare one for Lucy as well.
>>> def singLucy():
happy() happy()
print("Happy birthday, dear Lucy. ") happy()
Now we can write a
mainprogram that sings to both Fred and Lucy:
>>> def main():
singFred() print()
singLucy()
The bare
printbetween the two function calls puts a space between the verses in our output. And here's the final product in action:
>>> main()
Happy birthday to you!
Happy birthday to you!
Happy birthday, dear Fred.
Happy birthday to you!
Happy birthday to you!
Happy birthday to you!
Happy birthday, dear Lucy.
Happy birthday to you!
Well now, that certainly seems to work, and we've removed some of the duplication by defining the
happyfunction. However, something still doesn't feel quite right. We have two functions,
singFredand
singLucy,that are almost identical. Following this approach, adding a verse for Elmer would have us create a
singElmerfunction that looks just like those for Fred and Lucy. Can't we do something about the proliferation of verses?
Notice that the only difference between
singFredand
singLucyis the name at the end of the third
printstatement. The verses are exactly the same except for this one changing part. We can collapse these two functions together by using a parameter. Let's write a generic function called
sing:179
>>> def sing(person):
happy() happy()
print ("Happy Birthday, dear" , person + " . ") happy()
This function makes use of a parameter named
person.A parameter is a variable that is initialized when the function is called. We can use the
singfunction to print a verse for either Fred or Lucy. We just need to supply the name as a parameter when we invoke the function:
>>> sing("Fred")
Happy birthday to you!
Happy birthday to you!
Happy Birthday, dear Fred.
Happy birthday to you!
>>> sing("Lucy")
Happy birthday to you!
Happy birthday to you!
Happy Birthday, dear Lucy.
Happy birthday to you!
Let's finish with a program that sings to all three of our birthday people:
>>> def main():
sing( "Fred") print()
sing( "Lucy") print()
sing("Elmer")
It doesn't get much easier than that.
Here is the complete program as a module file:
# happy.py def happy():
print("Happy Birthday to you!")
def sing(person):
happy() happy()
6.3. Future Value with a Function
print ("Happy birthday, dear", person + ". ") happy()
def main():
sing ("Fred") print()
sing ("Lucy") print()
sing ("Elmer") main()