[Tutor] defining functions.. why return?

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Thu, 6 Dec 2001 00:52:04 -0800 (PST)


On Wed, 5 Dec 2001, Frank Holmes wrote:

>    I am working my way thru "SAMS teach yourself Python in 24 hrs by Van 
> Laningham (funny, it seems to be taking me a lot more than 24 hrs...) and I 

Hmmm... You might actually find this page interesting: it's Peter Norvig's
essay about the books that claim to "Teach Yourself Foo in 24 Hours".

    http://norvig.com/21-days.html

Personally, I think it might take a bit longer than 24 hours to really
feel comfortable with Python.  But don't feel rushed about the whole
thing; there's no pop quizzes here or anything... *grin*



> have a question  concerning defining functions.
>    In his example (chapter 7 "functions and modules) Mr. Van Lanningham 
> shows the following example for defining a function:
> 
> 1.  def julian_leap (y):
> 2.  if (y%4)==0:
> 3.     return 1
> 4.  return 0
> 
>    Concerning the "return" statements, he says the interpreter keeps
> track of the place where the function was called and the return
> statement just means "goto the place in the code that you started
> from".

One analogy that might give the flavor of this "return" stuff: let's say
you're on the phone with a friend, and someone else calls you.  If you
have a nice phone, you can put your friend on hold, and answer the call.  
When you're done, you can switch and return back to your patiently waiting
friend.


In one sense, we can simulate this in Python:

###
>>> def chatWithFriend():
...    print "Anyway, I was about to knock on Knuth's door, when..."
...    print "Hold on, I'm getting a call."
...    putFriendOnHoldAndAnswer()
...    print "Hmmm... wrong number.  Anyway, where were we?"
...    return
... 
>>> def putFriendOnHoldAndAnswer():
...    print "hello?  hello?"
...    return
... 
>>> chatWithFriend()
Anyway, I was about to knock on Knuth's door, when...
Hold on, I'm getting a call.
hello?  hello?
Hmmm... wrong number.  Anyway, where were we?
###


"return" has to do with the idea that when we call
putFriendOnHoldAndAnswer() within chatWithFriend(), we want to make sure
we get back to resume the conversation.  In this case, after
putFriendOnHoldAndAnswer() is done, we're "return"ing back to where we
left off.



Functions are really nice because of this returning behavior: we can build
small functions that use other functions to do all the heavy lifting.  
Here's an example of using functions:

###
>>> def square(x):             
...     return x * x
... 
>>> def sqrt(x):
...     return x**0.5
... 
>>> def hypotenuse(side_a, side_b):
...     return sqrt(square(side_a) + square(side_b))
... 
>>> hypotenuse(3, 4)
5.0
###

The thing to see is that hypotenuse() is asking square() to give it the
squares of side_a and side_b.  square() is doing all these calculations,
and "returning" those values back to hypotenuse().  If we call square()
directly:

###
>>> square(42)
1764
###

that's ok, since it's returning its value back to us, the
interpreter.  And we can also call sqrt(), independent of anything else:

###
>>> sqrt(42)
6.4807406984078604
###

but what's really neat is what happens when we write functions that use
other functions: that's where "return" really comes into play.