CPSC 100, Spring 1997:
Answers to Quiz #6

This is the sixth quiz from the course Computer Science 100: Principles of Computer Science, taught by David Eck. The answers that are included here are sample answers only. There might be many other answers to a given problem that would also receive full credit. See the home page for the text, The Most Complex Machine, for more information about the course.

Question 1: Define the term dummy parameter, and give an example.

Answer: Parameters are used to pass information back and forth between a subroutine and the rest of the program. When a subroutine is written, a dummy parameter is used as a stand-in for the actual parameter that will be passed to the subroutine when the subroutine is called. A dummy parameter is a name used in the definition of a subroutine to represent whatever actual parameter is later passed to the subroutine. For example, in the subroutine SUB Tri(size) whose definition is given in Question 4 on this lab, size is a dummy parameter.

Note: In the statement forward(length), length is an actual parameter. It is not a dummy parameter. Similarly, when the subroutine Tri is called with the statement Tri(side_length), the variable side_length is being used as an actual parameter.

Question 2: Explain the term local variable. What are local variables? Where are they found? Why do they exist?

Answer: A local variable is a variable that is declared within a subroutine (as opposed to a global variable, which is declared outside of any subroutine). A local variable is part of the "black box." It represents a quantity that is needed only on the inside of the subroutine, so there is no need for the rest of the program to know about it. In fact, a local variable is completely invisible to the rest of the program. Local variables exist to help make subroutines more independent of the rest of the program. (You don't have to worry about whether a variable you want to use in the subroutine has the same name as a global variable, for example.)

Question 3: Explain what is meant by the maintenance stage of the software life cycle, and explain why maintenance is easier if the program is made up of modules.

Answer: Maintenance is the last stage of the software life cycle, in which a program is adapted to changing conditions and new requirements, and in which any newly-discovered errors are fixed. Maintenance can continue over a period of many years. Often, it costs much more to maintain a program than it did to write it in the first place, so it is important for a program to be written in a way that will make maintenance as easy as possible. Writing modular programs is one way to do this. A module is a component of a larger system that is relatively independent and which interacts with other parts of the system only in well-defined, easy-to-understand ways. If a program is constructed from modules, it will be possible to make changes in one module without affecting the rest of the program.

Question 4: The subroutine Tri(size), given below, draws a triangle whose side is size units long. It leaves the turtle back at its original heading and orientation. Use this subroutine to draw the picture shown. The picture consists of six triangles.

(six nexted triangles)
           SUB Tri(size)
              forward(size) turn(120)
              forward(size) turn(120)
              forward(size) turn(120)
           END SUB

Answer: Assume that the distance from one triangle to the next is one unit. This means that the triangles shrink by two units -- one unit on each end -- as you go from one triangle to the triangle just inside it. The lengths of the sides of the triangles can be taken as 12, 10, 8, 6, 4, and 2. There are many ways to write the program. You can use a LOOP or just draw each triangle separately. You can draw the triangles from the inside out or from the outside in. In all cases, however, you have to make sure to position the turtle properly before drawing each triangle. Here are three possible programs. (The complete program has to start with the definition of Tri as given above. Here, I just give the rest of the program, which uses Tri.)

   Tri(12)          DECLARE side_length                     DECLARE count, size
   forward(1)       side_length := 12                       count := 0
   Tri(10)          LOOP                                    size := 2
   forward(1)          Tri(side_length)                     LOOP
   Tri(8)              side_length := side_length - 2          Tri(size)
   forward(1)          EXIT if side_length = 0                 back(1)
   Tri(6)              forward(1)                              size := size + 2
   forward(1)       END LOOP                                   count := count + 1
   Tri(4)                                                      EXIT IF count = 6
   forward(1)                                               END LOOP

David Eck, 20 May 1997