WEEK05: functions 
---------------------------------------------------------------
 F: more functions...changing items in a list


HANDOUT: functionWorksheet.py


 - take 5 minutes and write down what you think the output of
   the program will be (work with your neighbor if you want)


#
#  what is the output of this program???
#  (what will it print to the screen?)
#
# ------------------------------------------------------ #
def forceInRange(alist, low, high):
  for i in range(len(alist)):
    if alist[i] < low:
      alist[i] = low
    elif alist[i] > high:
      alist[i] = high
# ------------------------------------------------------ #
def lowercase(text):
  lowtext = ""
  for ch in text:
    if ch.isupper():
      ch = ch.lower()
    lowtext += ch
  text = lowtext
# ------------------------------------------------------ #
def printdivider(ch):
  print 50*ch
# ------------------------------------------------------ #
def oddfunction():
  for i in range(1000):
    print "low = %d    high = %d " % (low, high)
# ------------------------------------------------------ #
def main():
  printdivider("!")
  reds = [22,-1,103,93,250,273,298,37]
  low = 0
  high = 255
  forceInRange(reds, low, high) 
  print "back in main...reds = ", reds

  returnvalue = printdivider(":")
  print "returnvalue = ", returnvalue
  returnvalue = printdivider(":")

  text = "we LOVE comp sci!!"
  lowercase(text)
  print "back in main...text = ", text
  printdivider("!")
  oddfunction()
# ------------------------------------------------------ #
main()


 - Let's work through this, writing out what the stack would
   look like as the program runs

 - here's forceInRange():
 
def forceInRange(alist, low, high):
  for i in range(len(alist)):
    if alist[i] < low:
      alist[i] = low
    elif alist[i] > high:
      alist[i] = high

 - here's a picture of the stack when forceInRange() is executing:

              stack
           |              |
           |              |
           ----------------
           |forceInRange()|
           |              |
           |  high ------------------------------------------------
           |  low -------------------------------------------      |
           |  alist -------------                            |     |
           |              |     |                            |     |
           |              |     |                            |     |
           ----------------     |                            |     |
           |main()        |     |                            |     |
           |              |     |                            |     |
           |              |     V                            |     |
           |  reds --------> [22,-1,103,93,250,273,298,37]   V     |
           |  low -----------------------------------------> 0     V
           |  high ---------------------------------------------> 255
           |              |     
           |              |
           |              |
           ----------------

 - notice that alist and reds point to the same list, so assigning
   a new value to alist[i] changes reds, too!!!!

SCOPE:
 - where in a program a given variable may be referenced or used
 - the variables in any function are **local** to that function,
   and do not exist outside of the function!!

UPDATE21: 

 - run update21 to get a copy of the functionWorksheet.py program
 - run the program and make sure you understand what happens and why
 - copy the program to http://www.pythontutor.com and run it!


YOUR TURN (if time...):

 - write a drawGhost() function to be called from main like this:

      ghost = drawGhost(gw,p,size)

   it should draw a ghost to the given graphics window, at point
   p, with the given size


    ___
   /   \
  / o o \
  |     |
  |   O |  Boo!
  |     |
  |     |