17 Procedures Mark Dixon So CCE SOFT 131
- Slides: 23
17 – Procedures Mark Dixon, So. CCE SOFT 131 Page 1
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, So. CCE SOFT 131 Page 2
Data Types • Integer – whole numbers • Long – whole numbers (large) • Single – decimal numbers • Double – decimal numbers (more precise) • Currency – money • String – text Mark Dixon, So. CCE SOFT 131 Page 3
Data Type Selection Mark Dixon, So. CCE SOFT 131 Page 4
Variables • Data types used in variable declaration: Dim x As Long Dim s As String Dim height As Double Dim ages(1 To 5) As Long Mark Dixon, So. CCE SOFT 131 Page 5
Large Programs • Real programs get very large • Exponential increase in effort A B C D Mark Dixon, So. CCE 1 (A) 3 (A, B, AB) 6 (A, B, C, AB, AC, BC) 10 (A, B, C, D, AB, AC, BC, AD, BD, CD) SOFT 131 Page 6
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, So. CCE SOFT 131 Page 7
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 maintain Mark Dixon, So. CCE SOFT 131 Page 8
General Procedures (how) • Definition: [Public|Private] Sub name() [Statementblock] End Sub • Call: Call name () or name Mark Dixon, So. CCE SOFT 131 Page 9
Procedures Mark Dixon, So. CCE SOFT 131 Page 10
Questions: Procedures • Write a line of code that calls the following procedure: Sub Thing() x = 24 End Sub Thing • Add lines of code around the following code to define a procedure: Sub Circ() pic. Main. Cls pic. Main. Circle (800, 800), 100 End Sub Mark Dixon, So. CCE SOFT 131 Page 11
Example: Hotel Rooms v 1 Option Explicit Const Room. Cost = 32. 5 Dim Rooms As Integer Dim Nights As Integer Dim Total. Cost As Single Private Sub btn. Calc_Click() Rooms = Val(txt. Rooms. Text) Nights = Val(txt. Nights. Text) Total. Cost = Rooms * Nights * Room. Cost lbl. Cost. Caption = "£" & Total. Cost End Sub Input Process Output result of operations should be visible immediately! Shneiderman 1998, p. 205 Hotel Rooms v 1 Mark Dixon, So. CCE SOFT 131 Page 12
Example: Hotel Rooms v 2 Option Explicit Const Room. Cost = 32. 5 Dim Rooms As Integer Dim Nights As Integer Dim Total. Cost As Single Private Sub Form_Load() Rooms = Val(txt. Rooms. Text) Nights = Val(txt. Nights. Text) Total. Cost = Rooms * Nights * Room. Cost lbl. Cost. Caption = "£" & Total. Cost End Sub Private Sub txt. Rooms_Change() Rooms = Val(txt. Rooms. Text) Nights = Val(txt. Nights. Text) Total. Cost = Rooms * Nights * Room. Cost lbl. Cost. Caption = "£" & Total. Cost End Sub Hotel Rooms v 2 Mark Dixon, So. CCE Private Sub txt. Nights_Change() Rooms = Val(txt. Rooms. Text) Nights = Val(txt. Nights. Text) Total. Cost = Rooms * Nights * Room. Cost lbl. Cost. Caption = "£" & Total. Cost End Sub SOFT 131 Duplicate 23 lines Page 13
Example: Hotel Rooms v 3 Option Explicit Const Room. Cost = 32. 5 Dim Rooms As Integer Dim Nights As Integer Dim Total. Cost As Single Private Sub Calculate() Rooms = Val(txt. Rooms. Text) Nights = Val(txt. Nights. Text) Total. Cost = Rooms * Nights * Room. Cost lbl. Cost. Caption = "£" & Total. Cost End Sub Private Sub Form_Load() Calculate End Sub Private Sub txt. Rooms_Change() Calculate End Sub Duplicate Calls, not Code Private Sub txt. Nights_Change() Calculate End Sub Hotel Rooms v 3 Mark Dixon, So. CCE 20 lines SOFT 131 Page 14
Example: Face v 2 Private Sub btn. Draw_Click() pic. Face. Cls pic. Face. Circle (2400, 2400), 2000 If chk. Nose. Value = vb. Checked Then pic. Face. Line (2400, 2200)-Step(0, 600) End If If opt. Open. Value = True Then pic. Face. Circle (1600, 1600), 500 pic. Face. Circle (3200, 1600), 500 Else pic. Face. Line (1100, 1600)-Step(1000, 0) pic. Face. Line (2700, 1600)-Step(1000, 0) End If If opt. Happy. Value = True Then pic. Face. Circle (2400, 2400), 1200, , 3. 4, 6 Else pic. Face. Circle (2400, 4400), 1200, , 0. 6, 2. 5 End If End Sub Face v 2 Mark Dixon, So. CCE SOFT 131 Page 15
Example: Face v 3 Private Sub Draw. Face() pic. Face. Cls pic. Face. Circle (2400, 2400), 2000 If chk. Nose. Value = vb. Checked Then pic. Face. Line (2400, 2200)-Step(0, 600) End If If opt. Open. Value = True Then pic. Face. Circle (1600, 1600), 500 pic. Face. Circle (3200, 1600), 500 Else pic. Face. Line (1100, 1600)-Step(1000, 0) pic. Face. Line (2700, 1600)-Step(1000, 0) End If If opt. Happy. Value = True Then pic. Face. Circle (2400, 2400), 1200, , 3. 4, 6 Else pic. Face. Circle (2400, 4400), 1200, , 0. 6, 2. 5 End If End Sub Private Sub Form_Load() Me. Show Draw. Face End Sub Private Sub chk. Nose_Click() Draw. Face End Sub Private Sub opt. Happy_Click() Draw. Face End Sub Face v 3 Mark Dixon, So. CCE SOFT 131 Private Sub opt. Open_Click() Draw. Face End Sub Page 16
Example: Face v 4 Private Sub Draw. Face() pic. Face. Cls pic. Face. Circle (2400, 2400), 2000 Draw. Nose Draw. Eyes Draw. Mouth End Sub Private Sub Draw. Nose() If chk. Nose. Value = vb. Checked Then pic. Face. Line (2400, 2200)-Step(0, 600) End If End Sub Private Sub Draw. Eyes() If opt. Open. Value = True Then pic. Face. Circle (1600, 1600), 500 pic. Face. Circle (3200, 1600), 500 Else pic. Face. Line (1100, 1600)-Step(1000, 0) pic. Face. Line (2700, 1600)-Step(1000, 0) End If End Sub Private Sub Draw. Mouth() If opt. Happy. Value = True Then pic. Face. Circle (2400, 2400), 1200, , 3. 4, 6 Else pic. Face. Circle (2400, 4400), 1200, , 0. 6, 2. 5 End If End Sub Private Sub Form_Load() Me. Show Draw. Face End Sub Face v 3 Mark Dixon, So. CCE SOFT 131 … Page 17
Module Hierarchy Charts Private Sub Draw. Face() pic. Face. Cls pic. Face. Circle (2400, 2400), 2000 Draw. Face Draw. Nose Draw. Eyes Draw. Mouth End Sub Draw Nose Private Sub Draw. Nose() If chk. Nose. Value = vb. Checked Then pic. Face. Line (2400, 2200)-Step(0, 600) End If Draw Eyes Draw Mouth End Sub Private Sub Draw. Eyes() If opt. Open. Value = True Then pic. Face. Circle (1600, 1600), 500 pic. Face. Circle (3200, 1600), 500 Else pic. Face. Line (1100, 1600)-Step(1000, 0) pic. Face. Line (2700, 1600)-Step(1000, 0) End If End Sub Private Sub Draw. Mouth() If opt. Happy. Value = True Then pic. Face. Circle (2400, 2400), 1200, , 3. 4, 6 Else pic. Face. Circle (2400, 4400), 1200, , 0. 6, 2. 5 End If End Sub Private Sub Form_Load() Me. Show Draw. Face End Sub Mark Dixon, So. CCE … SOFT 131 Page 18
Example: Heart Rate Option Explicit Dim 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) = 141 End Sub Mark Dixon, So. CCE SOFT 131 Private Sub btn. Draw_Click() Dim i As Long pic. Main. Cls For i = 0 To 6 pic. Main. Line -(i * 500, HR(i) * 10) Next Page 19 End Sub
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, So. CCE SOFT 131 Page 20
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, So. CCE SOFT 131 Page 21
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, So. CCE SOFT 131 Page 22
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. Mark Dixon, So. CCE SOFT 131 Page 23
- Mark dixon artist
- Mark dixon
- Cce 2332
- What is cce?
- Cce+
- Cce upea ciencias de la educacion
- Cce chautauqua
- Development of cce
- Cce curriculum
- Digital diary cce
- Cce upea
- Adecco relax
- A mason-dixon memory summary
- Dixon case study
- George washington dixon
- Jefferson dixon campus
- Paul dixon smu
- Potky
- Dixon montessori charter school
- Gda school
- Jennifer dixon accident
- Karen k dixon
- Grafico de dixon
- David dixon ubc