[Tutor] Focus on the functions [Was Re: if-else statements]

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sun Oct 16 02:02:09 CEST 2005



On Sat, 15 Oct 2005, Kent Johnson wrote:
> > I really wish that someone would take a similar approach in writing a
> > beginner Python tutorial that focuses on functions almost immediately,
> > even before control flow.
>
> You might like John Zelle's book - he teaches functions before control
> flow and puts a fair emphasis on using functions to eliminate
> duplication and improve program structure.
>
> "Python Programming: An Introduction to Computer Science"
> http://www.fbeedle.com/99-6.html

Hi Kent,

Thanks for the book reference; it's nice that some portions of the book
are available in PDF format.


I apologize to the list again for the noisy contentless ranting I made
yesterday, so I'll try making it up by doing concrete demos of a
function-focused approach.  This will critique Zelle's Chapter Two and
Chapter Five, and see what things would look like if under a function
regime.


The 'convert.py' program on Chapter 2 is of the form:

######
# convert.py
#     A program to convert Celsius tmps to Fahrenheit
# by: Susan Computewell

def main():
    celsius = input("What is the Celsius temperature? ")
    fahrenheit = (9.0 / 5.0) * celsius + 32
    print "The temperature is", fahrenheit, "degrees Fahrenheit"

main()
######

It would have been nicer to strip out the input/output stuff altogether,
and just straight into showing a to_fahrenheit() function:

#######
def to_fahrenheit(celsius):
    """A function to convert celsius tmps to Fahrenheit."""
    return (9.0 / 5.0) * celsius + 32
#######


This function can then be used from the interactive interpreter, because
the interactive interpreter prompt itself serves as a good place to do
program interaction:

######
>>> to_fahrenheit(0)
32.0
>>> to_fahrenheit(100)
212.0
######


If we're going to take advantage of any Python-specific feature, it might
as well be the interactive interpreter prompt.  One of Python's greatest
strengths is that it has a good interactive interpreter prompt that we can
use for casual experimentation.

My feeling is that 'input()' and 'print' isn't really useful until we get
to the point of writing programs that other people are meant to use.
Until then, the interactive interpreter is sufficient as our 'user
interface' to the program.



As another example, something like the average program:

######
def main():
    print "This program computes the average of two exam scores"
    score1, score2 = input("Enter two scores separated by commas")
    average = (score1 + score2) / 2.0
    print "The average of the scores is:", average
main()
######


could be reduced to a small function:

######
def average(score1, score2):
    """Computes the average of two exam scores."""
    return (score1 + score2) / 2.0
######



Another obvious example where an earlier introduction of functions would
have helped would be the example on Chapter 5 on drawing two "eyes":

######
leftEye = Circle(Point(80, 50), 5)
leftEye.setFill('yellow')
leftEye.setOutline('red')
rightEye = Circle(Point(100, 50), 5)
rightEye.setFill('yellow')
rightEye.setOutline('red')
######

The approach advocated by the book was to know that certain objects could
be cloned() through the graphics API:

######
leftEye = Circle(Point(80, 50), 5)
leftEye.setFill('yellow')
leftEye.setOutline('red')
rightEye = leftEye.clone()
rightEye.move(20, 0)
######

But to me, this is a "trick" in the sense that it's teaching a technique
that doesn't apply to situations in general --- if we didn't have clone(),
what would we have done then?


A more useful approach would have been to use functions:

######
def Eye(centerx, centery):
    """Builds a new eye at the point given by (centerx, centery)."""
    eye = Circle(Point(centerx, centery), 5)
    eye.setFill('yellow')
    eye.setOutline('red')
    return eye

leftEye = Eye(80, 50)
rightEye = Eye(100, 50)
######

An Eye() definition allows us to go crazy with Mr. Potatohead without
having to think so much about the individual atomic steps we have to do to
build an eye each time. If we want a triclops, then building another eye
is just a matter of calling Eye() with a different center, just as
building a Circle is a matter of providing a Point and a size.


I do see that after functions are introduced that things start to pick up.
But I really do think that writing functions should have come up in
Chapter One or Two, and not as late in the game as Chapter Six.  That's
what I don't get.


Anyway, hope this helps!



More information about the Tutor mailing list