Mark Dixon, SoCCE SOFT 136Page 1 9 – Procedures. Mark Dixon, SoCCE SOFT 136Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved.

Slides:



Advertisements
Similar presentations
Mark Dixon Page 1 23 – Object Oriented Programming in PhP.
Advertisements

Mark Dixon, SoCCE SOFT 131Page 1 15 – Object Oriented Analysis, Design, and Programming.
Mark Dixon, SoCCE SOFT 131Page 1 22 – Object Oriented Analysis, Design, and Programming.
Tutorial #6 PseudoCode (reprise)
Mark Dixon Page 1 04 – Database Design: Forms. Mark Dixon Page 2 Session Aims & Objectives Aims –To allow easier data entry using forms Objectives, by.
Mark Dixon, SoCCE SOFT 131Page 1 09 – Procedures.
Mark Dixon Page 1 22 – Object Oriented Programming in ASP.
Mark Dixon, SoCCE SOFT 136Page 1 05 – Conditional Execution.
Mark Dixon, SoCCE SOFT 136Page 1 11 – User Defined Functions.
Mark Dixon, SoCCE SOFT 131Page 1 13 – Object Oriented Analysis, Design, and Programming.
Mark Dixon, SoCCE SOFT 131Page 1 07 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 06 – Iteration. Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –Revise Variables and Conditional execution.
Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 15 – Object Associations.
Mark Dixon Page 1 7 – Procedures. Mark Dixon Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved in grouping instructions,
Mark Dixon, SoCCE SOFT 131Page 1 03 – Debugging & Graphics.
Mark Dixon, SoCCE SOFT 131Page 1 06 – Iteration. Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Data-types, Variables, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 07 – Constants, Arrays, & Structures.
Mark Dixon, SoCCE SOFT 131Page 1 19 – Procedure Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 16 – Object Associations.
Mark Dixon Page 1 21 – Object Oriented Programming in ASP.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Procedures.
Mark Dixon, SoCCE SOFT 131Page 1 02 – Software Development Lifecycle, & User Interface Design.
Mark Dixon, SoCCE SOFT 131Page 1 10 – Variable Scope, and Arrays of Structures.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Conditional Execution.
Mark Dixon Page 1 02 – Queries: Query by Example.
Mark Dixon, SoCCE SOFT 131Page 1 10 – User Defined Functions.
Mark Dixon, SoCCE SOFT 131Page 1 12 – Enumerated Data-Types & Pass-by-reference.
Mark Dixon, SoCCE SOFT 131Page 1 13 – Control Arrays & Container Controls.
Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables.
Mark Dixon, SoCCE SOFT 131Page 1 09 – User Defined Procedures: Scope, and Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 11 – Arrays of Structures & Modules.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Information Processing: Data-types, Variables, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 7 – Procedures. Mark Dixon, SoCCE SOFT 131Page 2 Coursework 1 (Test) - Results Student Number Score (max 50) Classification.
Mark Dixon Page 1 04 – Data Types. Mark Dixon Page 2 Admin: On-line Quiz.
Mark Dixon Page 1 10 – Iterative Execution. Mark Dixon Page 2 Questions: Variables Write a line of code to declare a variable called h Write a line of.
Mark Dixon Page 1 24 – Object Oriented Programming in ASP.
Selection: IF Statement Damian Gordon. adsdfsdsdsfsdfs dsdlkmfsdfmsdl kfsdmkfsldfmsk dddfsdsdfsd.
Mark Dixon Page 1 10 – Procedures. Mark Dixon Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved in grouping instructions,
A PowerPoint about Algorithm’s. What is an algorithm?  a process or set of rules to be followed in calculations or other problem-solving operations,
Week 2 Fancy Face, Conditional Execution, Recursive Tree Computer Science I Scott C Johnson Fall
Mark Dixon, SoCCE SOFT 131Page 1 05 – Variables. Mark Dixon, SoCCE SOFT 131Page 2 Admin: Test (next week) In class test –teaching week 6 –university week.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.
Top-Down Design Damian Gordon. Top-Down Design Top-Down Design (also known as stepwise design) is breaking down a problem into steps. In Top-down Design.
Mark Dixon, SoCCE SOFT 131Page 1 23 – Object Associations.
Mark Dixon, SoCCE SOFT 131Page 1 24 – Datatypes and Object Association.
Mark Dixon Page 1 09 – Problem Solving. Mark Dixon Page 2 Admin: Test 1 Marked One-to-one debriefs, you can –Look at your script –Look at mark scheme.
Mark Dixon Page 1 8 – Procedures. Mark Dixon Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved in grouping instructions,
Sequence Damian Gordon. Pseudocode When we write programs, we assume that the computer executes the program starting at the beginning and working its.
Mark Dixon 1 9 – Case Study. Mark Dixon 2 Session Aims and Objectives Aims –To give an overview of the development of a web-page from initial idea to.
Mark Dixon, SoCCE SOFT 131Page 1 18 – Enumerated Data Types and Arrays of Structures.
Mark Dixon SOFT 131Page 1 12 – Object Oriented Analysis, Design, and Programming.
Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2.
Mark Dixon Page 1 05 – Problem Solving & Data Types.
Mark Dixon Page 1 15 – Structured Programming. Mark Dixon Page 2 Admin: Coursework 3 – Test In class test –9 Feb 2010 –2 Feb 2010: revision (technique)
Mark Dixon, SoCCE SOFT 131Page 1 17 – Procedures.
Mark Dixon 1 15 – Structured Programming. Mark Dixon 2 Admin: Test 2 In class test –11 Feb 2014 –4 Feb 2014: revision (technique) session 50 mins short.
Mark Dixon 1 13 – Parameters. Mark Dixon 2 Question: Arrays How many array variables are in the following code: Dim x Dim y Dim f(4) x = 12 y = 6 f(2)
Mark Dixon, School of Computing SOFT 120Page 1 1. User Defined Procedures.
Pseudocode Skill Area Materials Prepared by Dhimas Ruswanto, BMm.
Mark Dixon Page 1 09 – Iterative Execution. Mark Dixon Page 2 Questions: Variables Write a line of VBScript code to declare a variable called h Write.
Mark Dixon, SoCCE SOFT 136Page 1 08 – Arrays & Structures.
11 – Procedures.
11 – Enumerated Data Types, & Procedure Parameters
23 – Object Oriented Programming in ASP
Algorithms Y10 Introduction.
Department Array in Visual Basic
Presentation transcript:

Mark Dixon, SoCCE SOFT 136Page 1 9 – Procedures

Mark Dixon, SoCCE SOFT 136Page 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 136Page 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)

Mark Dixon, SoCCE SOFT 136Page 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 136Page 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 136Page 6 General Procedures (how) Definition: Sub name() [Statementblock] End Sub Call: name

Mark Dixon, SoCCE SOFT 136Page 7 Procedures

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

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

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

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

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

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

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

Mark Dixon, SoCCE SOFT 136Page 16 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 Private Sub btnDraw_Click() Dim i As Long picMain.Cls For i = 0 To 6 picMain.Line -(i * 500, HR(i) * 10) Next End Sub

Mark Dixon, SoCCE SOFT 136Page 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 136Page 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 136Page 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 136Page 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.