Mark Dixon, SoCCE SOFT 131Page 1 20 – Web applications: HTML and Client-side code.
Mark Dixon, SoCCE SOFT 136Page 1 9 – Procedures. Mark Dixon, SoCCE SOFT 136Page 2 Session Aims &...
-
Upload
kristian-mcdaniel -
Category
Documents
-
view
212 -
download
0
Transcript of Mark Dixon, SoCCE SOFT 136Page 1 9 – Procedures. Mark Dixon, SoCCE SOFT 136Page 2 Session Aims &...
Mark Dixon, SoCCE SOFT 136 Page 1
9 – Procedures
Mark Dixon, SoCCE SOFT 136 Page 2
Session Aims & Objectives• Aims
– To introduce the main concepts involved in grouping instructions, to deal with large programs.
• Objectives,by end of this week’s sessions, you should be able to:
– define procedures, and– call procedures
Mark Dixon, SoCCE SOFT 136 Page 3
Large Programs• Real programs get very large
• Exponential increase in effort
A B
C D
1 (A) 3 (A, B, AB) 6 (A, B, C, AB, AC, BC)
10 (A, B, C, D, AB, AC, BC, AD, BD, CD)
Mark Dixon, SoCCE SOFT 136 Page 4
General Procedures (what?)• Group of ordered instructions
• Identified by unique name
• Almost all computer code procedures– mirror real life procedures
Making a cup of tea: 1. Fill the kettle with water 2. Plug the kettle in 3. Switch the kettle on 4. Wait for the kettle to boil 5. Put a tea bag into the cup 6. Add sugar to the cup 7. Stir 8. Add milk to the cup 9. Stir 10. Take the tea bag out
Mark Dixon, SoCCE SOFT 136 Page 5
General Procedures (why?)• Code reuse:
same code used in many places (reduces duplication)
• Break up long code:large chunks of code are difficult to understand and maintain
Mark Dixon, SoCCE SOFT 136 Page 6
General Procedures (how)• Definition: Sub name() [Statementblock] End Sub
• Call: name
Mark Dixon, SoCCE SOFT 136 Page 7
Procedures
Mark Dixon, SoCCE SOFT 136 Page 8
Questions: Procedures• Write a line of code that calls the following
procedure: Sub Thing()
x = 24
End Sub
• Add lines of code around the following code to define a procedure:
picMain.Cls
picMain.Circle (800,800), 100
Thing
Sub Circ()
End Sub
Mark Dixon, SoCCE SOFT 136 Page 9
Example: Hotel Rooms v1
Option ExplicitConst RoomCost = 32.5Dim Rooms As IntegerDim Nights As IntegerDim TotalCost As Single
Private Sub btnCalc_Click() Rooms = Val(txtRooms.Text) Nights = Val(txtNights.Text)
TotalCost = Rooms * Nights * RoomCost
lblCost.Caption = "£" & TotalCostEnd Sub
Input
Process
Output
Hotel Rooms v1
result of operations should be visible immediately!Shneiderman 1998, p. 205
Mark Dixon, SoCCE SOFT 136 Page 10
Example: Hotel Rooms v2Option ExplicitConst RoomCost = 32.5Dim Rooms As IntegerDim Nights As IntegerDim TotalCost As Single
Private Sub Form_Load() Rooms = Val(txtRooms.Text) Nights = Val(txtNights.Text) TotalCost = Rooms * Nights * RoomCost lblCost.Caption = "£" & TotalCostEnd Sub
Private Sub txtRooms_Change() Rooms = Val(txtRooms.Text) Nights = Val(txtNights.Text) TotalCost = Rooms * Nights * RoomCost lblCost.Caption = "£" & TotalCostEnd Sub
Private Sub txtNights_Change() Rooms = Val(txtRooms.Text) Nights = Val(txtNights.Text) TotalCost = Rooms * Nights * RoomCost lblCost.Caption = "£" & TotalCostEnd SubHotel Rooms v2
Duplicate
Duplicate
Duplicate
23 lines
Mark Dixon, SoCCE SOFT 136 Page 11
Example: Hotel Rooms v3Option ExplicitConst RoomCost = 32.5Dim Rooms As IntegerDim Nights As IntegerDim TotalCost As Single
Private Sub Calculate() Rooms = Val(txtRooms.Text) Nights = Val(txtNights.Text) TotalCost = Rooms * Nights * RoomCost lblCost.Caption = "£" & TotalCostEnd Sub
Private Sub Form_Load() CalculateEnd Sub
Private Sub txtRooms_Change() CalculateEnd Sub
Private Sub txtNights_Change() CalculateEnd Sub
DuplicateCalls,not Code
Hotel Rooms v320 lines
Mark Dixon, SoCCE SOFT 136 Page 12
Example: Face v2Private Sub btnDraw_Click() picFace.Cls picFace.Circle (2400, 2400), 2000 If chkNose.Value = vbChecked Then picFace.Line (2400, 2200)-Step(0, 600) End If If optOpen.Value = True Then picFace.Circle (1600, 1600), 500 picFace.Circle (3200, 1600), 500 Else picFace.Line (1100, 1600)-Step(1000, 0) picFace.Line (2700, 1600)-Step(1000, 0) End If If optHappy.Value = True Then picFace.Circle (2400, 2400), 1200, , 3.4, 6 Else picFace.Circle (2400, 4400), 1200, , 0.6, 2.5 End IfEnd Sub
Face v2
Mark Dixon, SoCCE SOFT 136 Page 13
Example: Face v3Private Sub DrawFace() picFace.Cls picFace.Circle (2400, 2400), 2000
If chkNose.Value = vbChecked Then picFace.Line (2400, 2200)-Step(0, 600) End If
If optOpen.Value = True Then picFace.Circle (1600, 1600), 500 picFace.Circle (3200, 1600), 500 Else picFace.Line (1100, 1600)-Step(1000, 0) picFace.Line (2700, 1600)-Step(1000, 0) End If
If optHappy.Value = True Then picFace.Circle (2400, 2400), 1200, , 3.4, 6 Else picFace.Circle (2400, 4400), 1200, , 0.6, 2.5 End If
End Sub
Private Sub Form_Load() Me.Show DrawFaceEnd Sub
Private Sub chkNose_Click() DrawFaceEnd Sub
Private Sub optHappy_Click() DrawFaceEnd Sub
Private Sub optOpen_Click() DrawFaceEnd Sub
Face v3
Mark Dixon, SoCCE SOFT 136 Page 14
Example: Face v4Private Sub DrawFace() picFace.Cls picFace.Circle (2400, 2400), 2000
DrawNose DrawEyes DrawMouthEnd Sub
Private Sub DrawNose() If chkNose.Value = vbChecked Then picFace.Line (2400, 2200)-Step(0, 600) End If
End Sub
Private Sub DrawEyes() If optOpen.Value = True Then picFace.Circle (1600, 1600), 500 picFace.Circle (3200, 1600), 500 Else picFace.Line (1100, 1600)-Step(1000, 0) picFace.Line (2700, 1600)-Step(1000, 0) End If
End Sub
Private Sub DrawMouth() If optHappy.Value = True Then picFace.Circle (2400, 2400), 1200, , 3.4, 6 Else picFace.Circle (2400, 4400), 1200, , 0.6, 2.5 End If
End Sub
Private Sub Form_Load() Me.Show DrawFaceEnd Sub
…
Face v3
Mark Dixon, SoCCE SOFT 136 Page 15
Module Hierarchy ChartsPrivate Sub DrawFace() picFace.Cls picFace.Circle (2400, 2400), 2000
DrawNose DrawEyes DrawMouthEnd Sub
Private Sub DrawNose() If chkNose.Value = vbChecked Then picFace.Line (2400, 2200)-Step(0, 600) End If
End Sub
Private Sub DrawEyes() If optOpen.Value = True Then picFace.Circle (1600, 1600), 500 picFace.Circle (3200, 1600), 500 Else picFace.Line (1100, 1600)-Step(1000, 0) picFace.Line (2700, 1600)-Step(1000, 0) End If
End Sub
Private Sub DrawMouth() If optHappy.Value = True Then picFace.Circle (2400, 2400), 1200, , 3.4, 6 Else picFace.Circle (2400, 4400), 1200, , 0.6, 2.5 End If
End Sub
Private Sub Form_Load() Me.Show DrawFaceEnd Sub
…
DrawFace
DrawEyes
DrawNose
DrawMouth
Mark Dixon, SoCCE SOFT 136 Page 16
Example: Heart RateOption ExplicitDim HR(0 To 6) As Long
Private Sub Form_Load() HR(0) = 134 HR(1) = 127 HR(2) = 139 HR(3) = 155 HR(4) = 143 HR(5) = 151 HR(6) = 141End Sub
Private Sub btnDraw_Click()Dim i As Long picMain.Cls For i = 0 To 6 picMain.Line -(i * 500, HR(i) * 10) NextEnd Sub
Mark Dixon, SoCCE SOFT 136 Page 17
Tutorial Exercises: Hotel Rooms• Task 1: Get the Hotel Rooms examples versions
1, 2, and 3 (from the lecture) working.• Task 2: Modify your code – to give the result 0 if
the user enters a negative number for either number of rooms or number of nights.
Mark Dixon, SoCCE SOFT 136 Page 18
Tutorial Exercises: Face• Task 1: Get the Face examples versions 3 and 4
(from the lecture) working.• Task 2: Modify your code – use constants to
remove all magic numbers.
Mark Dixon, SoCCE SOFT 136 Page 19
Tutorial Exercises: Stick Man• Task 1: Modify your stick man program (from
previous week), to use procedures (decide which lines of code should be grouped together and what name should be used).
Mark Dixon, SoCCE SOFT 136 Page 20
Tutorial Exercises: Heart Rate• Task 1: Get the Heart Rate program (from the
lecture) working.• Task 2: Modify your program, to use procedures
(decide which lines of code should be grouped together and what name should be used).
• Task 3: Modify your program so that the first vertical line (from the origin 0,0) is not drawn.
• Task 4: Modify your program to draw horizontal lines at the 160 and 120 BPM marks.