knerr cs21 notes...

back to schedule

WEEK05: functions and lists
---------------------------------------------------------------
 W: review functions, scope, start lists

SPECIAL READING NOTE: section 6.2 (Incremental Development) from
	the Downey book is a *must* read!


REVIEW:

def happy():
    print "Happy Birthday to you!"

def sing(person):
    happy()
    happy()
    print "Happy birthday, dear", person + "."
    happy()

def main():
    sing("Fred")

 - how many parameters does the happy function have?
 - what does it return?

 - how many parameters does the sing function have?
 - what does it return?

 - in the call to sing in main, what is the argument?


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!!

 - here's the get_name() function from example_funcs.py:

def get_name():
  """
  The get_name function reads in a name entered by the user
  and returns it to the caller
  """
  uname = raw_input("enter your name: ")
  return uname

   you CAN NOT print uname from main -- it only exists in get_name().
   and you CAN NOT print a variable from main (like num or letter)
   here in get_name().

 - here's a picture of the stack when the get_name() function 
   is executing (just after I type in my name for the raw_input
   call):

              stack
           |           |
           |           |
           -------------
           |get_name() |
           |           |
           |  uname ---------> "jeff"
           |           |
           |           |
           -------------
           |main()     |
           |           |
           |  name     |
           |  num      |
           |  letter   |
           |  num_times|
           -------------

 - when get_name is done, the name variable in main will point
   to "jeff" and the entire get_name() part of the stack will
   be lost/erased

 - if you need some value from main in a function, you need to send
   the value to the function via parameters (and call the function
   with the value as an argument)

 - if you need some value calculated in a function back in main, you
   need to return the value from the function to main (using the
   return call). Downey calls this a "fruitful" function :)


LISTS (needed for Lab 5):

 - empty list:               mylist = []
 - add to a list:            mylist.append(5)
 - can set up lists by hand: mylist = ["fish", "dog", "cat", "pony"]
 - lists we've already seen: x = range(10)

 - lists are a type of sequence, so we can use them in for loops:

         for i in range(15):

               or 

         for animal in mylist:

 - in Lab 5 you will create a list of graphics objects, then
   do the same thing to each object in the list (like move or rotate)

 - lists can be indexed and sliced:

       print mylist[2]
       print mylist[2:]

 - and lists are mutable: mylist[2] = 3.14159

YOUR TURN:

 - copy my grade_averageLIST.py code and see if you can add the
   readGrades, findMin, and findMax functions. This code should
   ask the user for some grades, store them in a list, then do
   some calculations on the grades (find the average, min, and max).

   When done, it should look something like this:

$ python grade_averageLIST.py

please enter # of grades: 5
grade  1: 60
grade  2: 65
grade  3: 80
grade  4: 85
grade  5: 99

[60, 65, 80, 85, 99]
grade average: 77.80
    min grade: 60.00
    max grade: 99.00