T-Th 9:05
or
T-Th 11:15
in Olin 155

CS 1110: Introduction to Computing Using Python

Fall 2012

Assignment 2:
Call Frames

Due IN CLASS on Tuesday, September 25th

This is a simple written assignment where you are going to diagram a few call frames. It should not take more than 15-20 minutes do to this assignment. So even if you are busy revising assignment 1, you should be able to do this assignment as well. With that said, keep track of the actual time that you spent on this assignment for the survey.

"Diagramming" is what we do in the lecture notes where we draw frames to represent function calls and folders to represent the objects. These are a way of giving us a visual representation of what is going on in the computer when it runs Python. The purpose of this assignment is to see if you understand function calls.


Before You Get Started

Grading Policy

The revise-and-resubmit cycle was only for the previous assignment. This assignment will receive a single grade, and there will be no revisions. So you should try to get it right the first time. With that said, this assignment is meant to be fairly straightforward. If you are having trouble, please consult someone for help.

Collaboration Policy

You may do this assignment with one other person. If you are going to work together, then form your group on CMS as soon as possible. This must be completed before you submit the assignment. Both people must do something to form the group. The first person proposes, and then the other accepts. You do not need to have the same partner as last time; you can change your partner with each assignment.

If you do this assignment with another person, you must work together. It is against the rules for one person to to fill out this assignment and just have the other person put their name at it. You should actively collaborate on this assignment.

You must register your partner in CMS even if you are planning to turn in your assignment during class. You may not collaborate with anyone in the class beyond your CMS-registered partner.

Getting Help

If you do not know where to start, if you do not understand how function calls work, or if you are completely lost, please see someone immediately. This can be the course instructor, a TA, or a consultant. Do not wait until the last minute. See the staff page for more information.


Diagramming Conventions

Most of our diagramming conventions follow the lecture notes. However, just to be safe, we will make our conventions explicit here.

Diagramming Variables

A diagram for a variable should include the variable name and a box for the value. For example, here is a diagram for the variable x (which currently contains the float 2.0):

Diagramming Objects

All values in Python are objects, including values of type int and float. So, in a sense, object and value are synonyms. However, we only bother to draw an object as a folder if the object is mutable; that is, the contents of the folder (not the variable with the name of the folder) can be changed.

None of the values for the built-in types of int, float, bool, or str are mutable. If you were to add 1 to the variable x shown above with the statement

  x = x + 1
then Python creates a new float and puts that in x; it does not try to make 2.0 equal to some other value. So far the only mutable objects we have seen are those with named attributes, like those with type Point or Window.

You will not be responsible for figuring out how to diagram an object as a folder in this assignment; if the object is mutable, and a folder is necessary, we will provide it for you. However, when you diagram an object you should obey the following conventions:

  • The folder name should be unique, be represented by a number, and go on the folder tab.

  • The type of the object should go in a box to the right

  • The attributes should be listed just below the tab name and diagrammed just like variables.

  • We will ignore object methods for now.

For example, a folder for an object for the type Point from Lab 3 might look like this:

Diagramming a Call Frame

Frames should obey the following conventions:

  • The top left corner is a box with the function name.

  • The top right counter is the instruction counter. This represents the next line of code to execute in the function or module.

  • The first line of code after the function header corresponds to a counter value of 1.

  • Local variables and parameters are diagrammed in the frame body; we do not distinguish between them.

  • Local variables are not added until the instruction counter reaches the line where they are first defined.

See the lecture notes for more details on how to create a call frame. In order to help you with the instruction counter, we will always number the lines of code for you. For example, suppose we have the procedure

  def print_name(last, first):
1      print "Hello "+first+" "+last+"!"

If we execute the call print_name("White","Walker"), then the call frame starts out as follows:

That is the state of the call frame when the function just starts, before it executes line 1 (remember, the instruction counter is the next line). When we execute line 1, the call frame is as follows:

Note the instruction counter is blank; that is because there are no more instructions to execute.


Assignment Instructions

Function min(a,b)

The function min is built into Python. However, if we were to design it ourselves, it might look like this:

   def min(a,b):
       """Returns: min of a and b"""
1      result = a
2      if (b < a):
3          result = b
4      return result

Diagram the Call min(1,2)

Draw a sequence of call frames illustrating how the call frame changes over time for the function call min(1,2). You will need to draw either four or five frames: the call frame when the function starts up, and one for each of the four lines of code in the function. The reason why you might draw four frames instead of five is that you will not draw a frame for line 3 if it is not executed.

When an assignment changes the value of an existing variable, we would like you to write the old value in the box, cross it out, and write the new value in the box. In addition, the instruction counter in the final version of the frame should be blank, indicating that it is the last line of code to be executed.

Diagram the Call min(b,a)

Now suppose that you have the following (global) variables:

As we said in class, these variables are not the same as the parameters of min, which are local to that function.

Repeat the previous exercise for the function call min(b,a). Remember that you should only draw a frame for line 3 if it is executed.


Function rotate(q)

The function rotate(q) function is a variation on the shift(q) function from Lab 3. It takes a point as input and swaps the x and z attributes; the y attribute is left unaltered. We define this function as follows:

   def rotate(q):
       """Swap the x and z attributes of q
       
       Example: The point (1,2,3) becomes (3,2,1)
       
       Precondition: q contains the name of a point object"""
1      tmp = q.x
2      q.x = q.z
3      q.z = tmp

Diagram the Call rotate(p)

Remember that a variable can never contain a mutable object; it can only hold the name of a mutable object. Since Point is mutable, the assignment

  p = Point(1,2,3)
would produce the following result:

In this case we have a folder representing the Point object, and p stores the name of this Point object. Diagram the frames for the call rotate(p) using the value of p shown above. This time you should draw four versions of the frame for the function call: once in the beginning, and once for each line executed.

Next to each frame you should draw the folder for the Point p (Do not draw the variable p! Just draw the folder whose name is in p). If the current instruction modifies the contents of the folder, you should indicate that (by crossing out the old value and writing in the new) when you draw the folder.


Turning in the Assignment

When you finish the assignment, put your name and netid at the top of the page. Otherwise, we will not know that the assignment is yours, and we cannot give you credit for your work. If you are working with a partner, then the partner's name must be on the assignment as well. In addition, to help us out with organization, please underline your last name(s).

You have two options for submitting this assignment. The first is to bring the assignment to class on the day it is due: Tuesday, September 25th. Alternatively, you may scan the file as a PDF and submit it to CMS before 5 pm that day. We will start grading this immediately, so please make sure the assignment is turned in on time.

Survey

Once again, this assignment comes with a survey on CMS. This survey asks how long you spent on the assignment, your impression of the difficulty, and what could be done to improve it.

Please try to complete the survey within a day of turning in this assignment. Remember that participation in surveys compromise 1% of your final grade. We also ask that you be honest in your answers.