1 |
h04 |
CSE SPIS 2016 |
Name: | ||||
---|---|---|---|---|
(full first and last name) | ||||
UCSD email address: | @ucsd.edu | |||
Optional: name you wish to be called if different from name above. | ||||
Optional: name of "homework buddy" (leaving this blank signifies "I worked alone" |
h04: Guttag, Sections 4.1, 4.2 4.3
ready? | assigned | due | points |
---|---|---|---|
true | Wed 08/03 09:00AM | Tue 08/09 09:00AM |
You may collaborate on this homework with AT MOST one person, an optional "homework buddy".
Bring this with you to your first lecture on the due date indicate.
Please read Sections 4.1, 4.2, and 4.3 of the Guttag textbook, then answer these questions.
-
(20 pts) On p. 34, Guttag contends that you can solve any problem that is computationally solvable using the programming constructs from Chapters 1-3. List four main reasons for using functions in your program. Write your answer based on your entire reading of Chapter 4, rather than the arguments provided by Guttag on p. 34 alone.
-
(20 pts) What is a function specification and why is it used in programming?
-
(20 pts) Explain via an example the basic ingredients of a recursive function implementation. Try to use an example that is different from those used in your reading.
-
(20 pts) In the implementation of the program given in Figure 4.8 of Guttag, what is the purpose of the helper function
isPal()
? -
In the following questions we will practice tracing code to understand how functions work. Code tracing is the process of reading a piece of code line by line and visualizing what would happen if it were to actually run on your computer. This is not only a way to test your understanding of programming concepts but also an essential tool for debugging your programs. Trace the code given below and answer the questions that follow.
def swap(x, y): tmp = x x = y y = tmp print x, y a = 3 b = 4 print a, b swap(a,b) print a, b
-
(4 pts) Identify the formal and actual parameters in the above code
-
(12 pts) Using diagrams similar to those in Figure 4.4 on page 39 of Guttag, show the state of the runtime stack and the values of the variables
a
,b
,x
andy
when the program execution is at the print statement specified in each of the following points in the code:(a) prior to the call to the swap
function(b) in the body of the the swap
function(c) following the call to the swap
function -
(4 pts) What are the values printed to console when the above code is executed?