Fall 2018

# Tue Sep 25

Discuss the homework due today, on strings, lists, and files.

I've posted my sample solutions and will go over them.

Upcoming schedule: no class Thursday ... so think next chapter homework, on functions, will be due Thursday next week rather than Tuesday.

## chap 5 - functions

This is some of the most important material of the term.

And I want to add a bit more stuff : doc strings & doc tests.

Topics to discuss :

• what are functions
• sub programs
• with inputs (arguments) and outputs (return value)
• "def" ....
• why use them
• split large tasks into smaller ones
• smaller tasks are easier to understand, document, and test
• avoid code repetition
• scope of variable definitions
• which variable names are defined in which context
• ... and which ones can be changed in which ways
• input parameters (and similarities / differences with "input()")
• return value (and similarities / differences with "print()")
• doc strings (not done in book)
• doc tests (not done in book) - here's an example
• optional arguments & default values (not done in book)

We'll look at lots of examples in class ... and with the online "visualize python" tool.

# An optional argument with a default value.
def scale(x, multiplier=2):
""" Return x * multiplier """   #  <= doc string
return multiplier * x
print("twice 10 is {}".format( scale(10) ))
print("triple 10 is {}".format( scale(10, 3) ))

# Is x modified ?
def double(y):
y = 2*y
return y
x = 10
double_x = double(x)
print("x is ", x)
print("double_x is ", double_x)

# Is numbers modified ?
def double_all(them):
for i in range(len(them)):
them[i] = 2 * them[i]
return them
numbers = [10, 11, 12]
double_numbers = double_all(numbers)
print("numbers is ", numbers)
print("double_numbers is ", double_numbers)


Hmmm. Run these in pythontutor.com to see exactly what's going on ...

https://cs.marlboro.college /cours /fall2018 /python /notes /chap6