CSE 203 Closed Lab 12 Instructions

Table of Contents


1. Objectives

To learn about defining new functions and new methods.


2. Set Up

  1. Two students should work together at one workstation.
  2. In one student's account, follow the instructions given below in section 3, Method.  Remember, trading roles (driver and non-driver) for each new session is a very good idea.

3. Method

  1. Start by opening "015 Mouse Handling.kpl" from the "1) Step-by-Step Tutorial" folder.
  2. Immediately Save As New Program with name "Our_Own_Functions.kpl" in folder "0) My Own".
  3. In a comment at the top of the file, add author name(s) and the date appropriately.
  4. Change the program name to "Our_Own_Functions" (if you haven't already done so).
  5. In Closed Lab 11, we learned how to change the program so that the action would happen when the mouse button was released.  Today, we'll learn how to change how our main program looks, or how it "reads," by calling new functions that we define ourselves.  One can define a new function (or method) by supplying its definition in the program.
  6. After method Main()'s "End Method", type the following:
        Function LeftReleased() As Boolean
            Return Mouse.Event = "ButtonUp" And LeftPressed
        End Function
  7. We've just told Phrogram that LeftReleased() is a new function that we're allowed to call.  We've told it that this function returns a Boolean (a true or false) value.  That means that it gives back a Boolean value in the place of any call made to it.  We've also told it what Boolean value to give back.  We've told it to give back true if, and only if, the most recent mouse event is ButtonUp and it was the left mouse button that was pressed.
  8. Now go up in method Main() and change "If LeftPressed Then" to "If LeftReleased() Then", run the program, and see if we've made our program have the better behavior that we wanted to have yesterday.
  9. Now do all the steps necessary to make a similar change for our use of RightPressed.
  10. We can also define a new method.  Let's define method JumpTo() whose job is to move the pen without drawing a line but to put the pen back in drawing mode.
  11. After method Main()'s "End Method", type the following:
        Method JumpTo( X As Decimal, Y As Decimal )
            Pen( False )
            MoveTo( X, Y )
            Pen( True )
        End Method
  12. We've just told Phrogram that JumpTo() is a new method that we're allowed to call.  We've told it that when this method is called it must be called with two parameters (or arguments), both of which are of type Decimal.  (Our intention is that those two parameters represent screen coordinates.)  We've also told Phrogram what sequence of instructions to run each time JumpTo() is called.
  13. Replace in method Main() the following sequence of three instructions with just one instruction, "JumpTo( MouseX, MouseY)":
    Pen( False )
    MoveTo( MouseX, MouseY )
    Pen( True )
  14. In this call, "JumpTo( MouseX, MouseY)", we refer to MouseX and MouseY as actual parameters.  In the definition of method JumpTo(), we refer to X and Y as formal parameters.  When Phrogram is executing our program, it matches the first formal parameter, X, with the first actual parameter, MouseX, and it matches the second formal parameters, Y, with the second actual parameter, MouseY.
  15. Run the program to see if the behavior is still what we want.
  16. When you work on your course project, if you find that you're writing or copying the same sequence of statements in several places in your program, you might find defining your own method useful.  If you want to know more about it, just ask!

4. Proctor Help

If you have a question or get stuck, raise your hand and one of the proctors will come by to chat.