CSE 203 Closed Lab 12 Instructions
Table of Contents
1. Objectives
To learn about defining new functions and new methods.
2. Set Up
- Two students should work together at one workstation.
- 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
- Start by opening "015 Mouse Handling.kpl" from the "1)
Step-by-Step Tutorial" folder.
- Immediately Save As New Program with name "Our_Own_Functions.kpl"
in folder "0) My Own".
- In a comment at the top of the file, add author name(s) and the
date appropriately.
- Change the program name to "Our_Own_Functions" (if you haven't
already done so).
- 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.
- After method Main()'s "End Method", type the following:
Function LeftReleased() As Boolean
Return Mouse.Event = "ButtonUp"
And LeftPressed
End Function
- 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.
- 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.
- Now do all the steps necessary to make a similar change for our
use of RightPressed.
- 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.
- 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
- 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.
- Replace in method Main() the following sequence of three
instructions with just one instruction, "JumpTo( MouseX, MouseY)":
Pen( False )
MoveTo( MouseX, MouseY )
Pen( True )
- 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.
- Run the program to see if the behavior is still what we want.
- 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.