Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Dixon, SoCCE SOFT 131Page 1 09 – Procedures.

Similar presentations


Presentation on theme: "Mark Dixon, SoCCE SOFT 131Page 1 09 – Procedures."— Presentation transcript:

1 Mark Dixon, SoCCE SOFT 131Page 1 09 – Procedures

2 Mark Dixon, SoCCE SOFT 131Page 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

3 Mark Dixon, SoCCE SOFT 131Page 3 Large Programs Real programs get very large Exponential increase in effort AB CD 1 (A) 3 (A, B, AB) 6 (A, B, C, AB, AC, BC) 10 (A, B, C, D, AB, AC, BC, AD, BD, CD)

4 Mark Dixon, SoCCE SOFT 131Page 4 General Procedures (what?) Group of statements 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

5 Mark Dixon, SoCCE SOFT 131Page 5 Physical Procedure Demo

6 Mark Dixon, SoCCE SOFT 131Page 6 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

7 Mark Dixon, SoCCE SOFT 131Page 7 General Procedures (how) Definition: [Public|Private] Sub name() [Statementblock] End Sub Call: Call name () or name Procedure

8 Mark Dixon, SoCCE SOFT 131Page 8 Exercise 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

9 Mark Dixon, SoCCE SOFT 131Page 9 Example: Hotel Rooms v1 Option Explicit Const RoomCost = 32.5 Dim Rooms As Integer Dim Nights As Integer Dim TotalCost As Single Private Sub btnCalc_Click() Rooms = Val(txtRooms.Text) Nights = Val(txtNights.Text) TotalCost = Rooms * Nights * RoomCost lblCost.Caption = "£" & TotalCost End Sub Input Process Output Hotel Rooms v1 result of operations should be visible immediately! Shneiderman 1998, p. 205

10 Mark Dixon, SoCCE SOFT 131Page 10 Example: Hotel Rooms v2 Option Explicit Const RoomCost = 32.5 Dim Rooms As Integer Dim Nights As Integer Dim TotalCost As Single Private Sub Form_Load() Rooms = Val(txtRooms.Text) Nights = Val(txtNights.Text) TotalCost = Rooms * Nights * RoomCost lblCost.Caption = "£" & TotalCost End Sub Private Sub txtRooms_Change() Rooms = Val(txtRooms.Text) Nights = Val(txtNights.Text) TotalCost = Rooms * Nights * RoomCost lblCost.Caption = "£" & TotalCost End Sub Private Sub txtNights_Change() Rooms = Val(txtRooms.Text) Nights = Val(txtNights.Text) TotalCost = Rooms * Nights * RoomCost lblCost.Caption = "£" & TotalCost End Sub Hotel Rooms v2 Duplicate 23 lines

11 Mark Dixon, SoCCE SOFT 131Page 11 Example: Hotel Rooms v3 Option Explicit Const RoomCost = 32.5 Dim Rooms As Integer Dim Nights As Integer Dim TotalCost As Single Private Sub Calculate() Rooms = Val(txtRooms.Text) Nights = Val(txtNights.Text) TotalCost = Rooms * Nights * RoomCost lblCost.Caption = "£" & TotalCost End Sub Private Sub Form_Load() Calculate End Sub Private Sub txtRooms_Change() Calculate End Sub Private Sub txtNights_Change() Calculate End Sub Duplicate Calls, not Code Hotel Rooms v3 20 lines

12 Mark Dixon, SoCCE SOFT 131Page 12 Example: Face v2 Private 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 If End Sub Face v2

13 Mark Dixon, SoCCE SOFT 131Page 13 Example: Face v3 Private 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 DrawFace End Sub Private Sub chkNose_Click() DrawFace End Sub Private Sub optHappy_Click() DrawFace End Sub Private Sub optOpen_Click() DrawFace End Sub Face v3

14 Mark Dixon, SoCCE SOFT 131Page 14 Example: Face v4 Private Sub DrawFace() picFace.Cls picFace.Circle (2400, 2400), 2000 DrawNose DrawEyes DrawMouth End 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 DrawFace End Sub … Face v3

15 Mark Dixon, SoCCE SOFT 131Page 15 Module Hierarchy Charts Private Sub DrawFace() picFace.Cls picFace.Circle (2400, 2400), 2000 DrawNose DrawEyes DrawMouth End 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 DrawFace End Sub … DrawFace Draw Eyes Draw Nose Draw Mouth


Download ppt "Mark Dixon, SoCCE SOFT 131Page 1 09 – Procedures."

Similar presentations


Ads by Google