Week 5: More Functions
Lab 4 available now.
Quiz 2 on Friday 4 October at the start of class.
Week 5 Topics
While loop practice
The list data type
Functions with lists
Recall the general template for writing a function:
def NAME(PARAMETER1, PARAMETER2, ...): """ Purpose Parameter description Return value description (if any) """ #DO STUFF return VALUE #optional
Exercise: Stack diagram
Now, try doing a stack diagram yourself. Show the stack diagram as it would appear at the point in the program noted below. In addition, show the output of the complete program.
dir() is a function that prints the name of all variables that are in scope. It is only for diagnostic purposes.
Stack Example 3
Try another example of stack drawing.
def absval(x,y): if x > y: aval = x-y else: aval = y-x # draw stack to this point! # as it would look just before return... return aval def main(): a = 10 b = 35 result = absval(a,b) #What variables are in scope? print(dir()) print("abs(%d-%d) = %d" % (a,b,result)) main()
What is the result of modifying the parameter variable for a function? Do the results effect the arguments in the original calling function, or are all changes ignored? We will explore this question in more depth this week.
Take a look at
oops_squareFunc.py, which squares the parameter values in a function. What do you think will happen at the end of this program? Test it out and then we will trace through the program to see what happened.
Immutable data types (
bool) can never be modified; thus,
any changes to the variable require reassigning it a new value. Reassignment
breaks any sharing of values between the parameter and its original argument.
Mutable data types are different - they can be modified without reassignment.
As an example, we will return to the
list datatype and see its methods.
Lists are a data type in Python that hold a collection of items. Lists are a type of
sequence of items. As we will see, many of the operations we use on strings can also
be used on lists, since both are sequences. There are two major differences
between lists and strings: lists are a sequence of items where the items can
be any of any type including (e.g.,
int, etc) while strings are strictly
a sequence of characters. The second meaningful difference is that lists are mutable,
meaning we can change the contents of the list. Strings are immutable.
We can do similar things to lists as with strings:
len(lst)number of items in list
lst[i]index into list to get ith item
An operation that is unique to lists is
append(), which adds an item to the list
(and thus changes its content)
lst = [0,10,20] lst.append(30) # adds 30 to lst
We can also iterate over lists just as we did with strings:
for i in range(len(lst)): # we can iterate over a list print lst[i]
One thing we can do with lists that we can’t do with strings is change a single element.
lst = [0,10,20] lst = 5 #legal. Changes first element of list print(lst) text = "hello" text = "j" #illegal. Strings are immutable
Side effects with mutable parameters
Previously, we showed that reassigning the parameter variable had no effect on the original argument that was sent to the function. This is always the case with immutable data types as once they are created, they cannot be modified.
However, there are circumstances where parameters of mutable data type (e.g., list) can be modified in a function, and the result will have the side effect of also changing the argument variable, despite not returning anything. This can be a desired outcome, or it could be the source of an error in what we want our rogram to do. Using stack traces to understand this phenomena will help us understand why.
in all circumstances, use of the assignment operator (
Exercise: list modification with side effects
squareList.py, we will trace through the program and show that, despite not having
a return value, our arguments are forever impacted by the call to the