04/02/20161 10.1 procedures top-down approach / stepwise refinement & sub procedures

29
21/06/22 21/06/22 1 10.1 Procedures 10.1 Procedures Top-down approach / Top-down approach / Stepwise Refinement Stepwise Refinement & Sub Procedures & Sub Procedures

Upload: hugh-hines

Post on 18-Jan-2018

219 views

Category:

Documents


0 download

DESCRIPTION

Top-Down Technique / Stepwise Refinement A problem solving technique: The problem is divided up into a number of smaller problems called modules. The problem is divided up into a number of smaller problems called modules. Each one is solved separately. Each one is solved separately. Then each module is combined to form a solution to the whole problem. Then each module is combined to form a solution to the whole problem.

TRANSCRIPT

Page 1: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

03/05/2303/05/23 11

10.1 Procedures10.1 ProceduresTop-down approach / Stepwise Top-down approach / Stepwise

Refinement Refinement & Sub Procedures& Sub Procedures

Page 2: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

2203/05/2303/05/23

Learning ObjectivesLearning Objectives

Define a procedure.Define a procedure.Explain why procedures are useful.Explain why procedures are useful.Explain the difference between event and Explain the difference between event and sub / function procedures.sub / function procedures.State how to begin and end a procedure.State how to begin and end a procedure.

Page 3: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

Top-Down TechniqueTop-Down Technique / Stepwise / Stepwise Refinement Refinement

A problem solving technique:A problem solving technique: The problem is divided up into a number of The problem is divided up into a number of

smaller problems called modules.smaller problems called modules. Each one is solved separately.Each one is solved separately. Then each module is combined to form a Then each module is combined to form a

solution to the whole problem.solution to the whole problem.

Page 4: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

4403/05/2303/05/23

How do we find the area of a 'house' made up from a square and

a triangle?

Page 5: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

5503/05/2303/05/23

Prose Prose (language description)(language description)

Find the area of the triangle by multiplying Find the area of the triangle by multiplying the base by the height and halving. Find the base by the height and halving. Find the area of the rectangle by multiplying the area of the rectangle by multiplying the width by the breadth. Then add the the width by the breadth. Then add the area of the triangle and rectangle area of the triangle and rectangle together.together. Each sentence is a module.Each sentence is a module.

Page 6: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

6603/05/2303/05/23

FormulaeFormulae BT = Base of the triangleBT = Base of the triangle HT = Height of the triangleHT = Height of the triangle W = Width of rectangleW = Width of rectangle BR = Breadth of rectangleBR = Breadth of rectangle AT = Area of the triangle AT = Area of the triangle AR = Area of the rectangle AR = Area of the rectangle

AT = ½ BHAT = ½ BH

AR = W + H + W + HAR = W + H + W + H= 2 * (H + W)= 2 * (H + W)

Area of the house = Area of the house = AT + ARAT + AR

Each formula is a module.Each formula is a module.

Page 7: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

7703/05/2303/05/23

Ordered StepsOrdered Steps

1.1. Find the height and base of the triangle.Find the height and base of the triangle.2.2. Find the area of the triangle by multiplying the Find the area of the triangle by multiplying the

height of the triangle by the base of the height of the triangle by the base of the triangle, and halving.triangle, and halving.

3.3. Find the width and breadth of the rectangle.Find the width and breadth of the rectangle.4.4. Find the area of the rectangle by adding the Find the area of the rectangle by adding the

width of the rectangle to the breadth of the width of the rectangle to the breadth of the rectangle, and multiplying by 2.rectangle, and multiplying by 2.

5.5. Add the areas of the triangle and Add the areas of the triangle and rectangle together.rectangle together.

Each step is a module.Each step is a module.

Page 8: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

8803/05/2303/05/23

FlowchartFlowchartOrdered steps using arrows to Ordered steps using arrows to point from one point from one instruction to the next instead of numbers.instruction to the next instead of numbers.

Find the height and base of the triangle.Find the height and base of the triangle.

Find the width and breadth of the rectangle.Find the width and breadth of the rectangle.

Add the areas of the triangle and rectangle together.Add the areas of the triangle and rectangle together.

Find the area of the triangle by multiplying the height of the Find the area of the triangle by multiplying the height of the triangle by the base of the triangle, and halving.triangle by the base of the triangle, and halving.

Find the area of the rectangle by adding the width of the Find the area of the rectangle by adding the width of the rectangle to the breadth of the rectangle, and multiplying by 2.rectangle to the breadth of the rectangle, and multiplying by 2.

Page 9: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

Using Using proceduresprocedures is the top-down is the top-down approach to programmingapproach to programming

Procedures are modules in program code:Procedures are modules in program code: A small subprogram which is given a name / A small subprogram which is given a name /

identifier.identifier. Does a defined task or combination of related Does a defined task or combination of related

defined tasks.defined tasks. Is identified by having a name and is Is identified by having a name and is

executed when called by its name / identifier.executed when called by its name / identifier.

Page 10: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

1010

Main Types of Procedure in VBMain Types of Procedure in VB

Event proceduresEvent procedures These are the ones you have used so far.These are the ones you have used so far. Executed in response to events e.g. click, change, …Executed in response to events e.g. click, change, …

Sub Sub proceduresprocedures (sometimes shortened to just (sometimes shortened to just proceduresprocedures))..Function procedures Function procedures (sometimes shortened to just (sometimes shortened to just functionsfunctions and these will be looked at in detail in and these will be looked at in detail in presentation presentation 10.3 Procedures)).. These are not set off directly by events, but are called These are not set off directly by events, but are called

by code within an event procedure or from within by code within an event procedure or from within another non-event procedure i.e. one of the above.another non-event procedure i.e. one of the above.

Page 11: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

111103/05/2303/05/23

Why use procedures?Why use procedures?Obviously most programs inevitably use event Obviously most programs inevitably use event procedures.procedures.You don’t You don’t havehave to use sub or function to use sub or function procedures but there are several procedures but there are several advantagesadvantages:: See the next slide.See the next slide.

Page 12: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

Top-Down Approach / Stepwise Top-Down Approach / Stepwise Refinement - AdvantagesRefinement - Advantages

Avoids repeating codeAvoids repeating code as modules can be stored in and as modules can be stored in and used used in other programs in other programs from a software library.from a software library.Makes the code more readable.Makes the code more readable.

By splitting a problem / code into smaller parts the solution is By splitting a problem / code into smaller parts the solution is easier to follow.easier to follow.

Helps in debugging a program.Helps in debugging a program.Fewer errors are likely to be made.Fewer errors are likely to be made.Any errors that are made will be easier to correct.Any errors that are made will be easier to correct.Many people can be involved in the solution.Many people can be involved in the solution.Individual skills can be used.Individual skills can be used.

The last 2 bold advantages should be used if an exam question The last 2 bold advantages should be used if an exam question scenario involves more than one person.scenario involves more than one person.

Page 13: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

Top-Down Approach / Stepwise Top-Down Approach / Stepwise Refinement - DisadvantagesRefinement - Disadvantages

Individual modules may work as required Individual modules may work as required but they may be linked incorrectly, so the but they may be linked incorrectly, so the links must be thoroughly tested.links must be thoroughly tested.Documentation of modules must be Documentation of modules must be thorough. thorough. Variables may clash across modules.Variables may clash across modules.Parameters may be of the wrong type.Parameters may be of the wrong type.

The last 2 disadvantages will become clear once you The last 2 disadvantages will become clear once you have covered presentation 10.2 Procedures.have covered presentation 10.2 Procedures.

Page 14: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

141403/05/2303/05/23

Writing and calling proceduresWriting and calling procedures

Sub and function procedures should be written Sub and function procedures should be written outside event proceduresoutside event procedures i.e. Where you declare global variables.i.e. Where you declare global variables.

i.e. Click below the line:i.e. Click below the line: Public Class Form1Public Class Form1

Press enter to make a blank line if necessary.Press enter to make a blank line if necessary.

As with naming controls and variables, no spaces As with naming controls and variables, no spaces are allowed.are allowed. We will use the same convention as for naming We will use the same convention as for naming

variables:variables:i.e. Each word starts with a capital i.e. Each word starts with a capital

Also remember to always use meaningful names.Also remember to always use meaningful names.

Page 15: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

Beginning proceduresBeginning procedures

To begin a procedure use:To begin a procedure use: Private Sub …()Private Sub …()

Procedure NameProcedure Name

PrivatePrivate means that the means that the procedure can only be used procedure can only be used on the form it is declared on on the form it is declared on (all the programs I will show you, (all the programs I will show you, use only one form anyway)use only one form anyway)..

Page 16: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

161603/05/2303/05/23

Ending proceduresEnding procedures

To end a procedure use:To end a procedure use: End SubEnd Sub

Page 17: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

171703/05/2303/05/23

Calling proceduresCalling procedures

To call a procedure use:To call a procedure use: Call …Call …

Procedure NameProcedure Name

You don’t actually have to use You don’t actually have to use CallCall..You can just the procedure’s name.You can just the procedure’s name.However, your code is more readable if you do However, your code is more readable if you do and it makes it easier to differentiate between a and it makes it easier to differentiate between a procedure and a variable identifier / name.procedure and a variable identifier / name.

Page 18: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

181803/05/2303/05/23

Program 10.1 Avoid repeating codeProgram 10.1 Avoid repeating code

Specification:Specification: Illustrate how procedures can make it Illustrate how procedures can make it

unnecessary to repeat code in two or more unnecessary to repeat code in two or more procedures.procedures.

Page 19: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

191903/05/2303/05/23

Program 10.1 Avoid repeating codeProgram 10.1 Avoid repeating code

Open the “Colour Change” Program you Open the “Colour Change” Program you wrote in wrote in 2.3 Working with controls2.3 Working with controls..

Make a copy of the previous program’s whole folder to keep the Make a copy of the previous program’s whole folder to keep the original program but rename this folder with the same name but original program but rename this folder with the same name but add add ((Procedure Version)Procedure Version)..

Each of the 3 scroll bars has a single line Each of the 3 scroll bars has a single line of identical code in their scroll events.of identical code in their scroll events.We will write this code only once in a We will write this code only once in a procedure.procedure.

Page 20: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

202003/05/2303/05/23

Program 10.1 Avoid repeating codeProgram 10.1 Avoid repeating code

Drag Drag oneone of the lines of identical code outside its of the lines of identical code outside its event procedure.event procedure. i.e. where you would declare global variables.i.e. where you would declare global variables.

i.e. Below the line:i.e. Below the line: Public Class Form1Public Class Form1

Press enter to make a blank line if necessary.Press enter to make a blank line if necessary.

Enter the following line before it:Enter the following line before it: Private Sub ShowFormColour() Private Sub ShowFormColour() ‘Declare the Sub procedure.‘Declare the Sub procedure.

Enter the following line after it:Enter the following line after it: End Sub End Sub ‘End the Sub procedure.‘End the Sub procedure.

Page 21: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

212103/05/2303/05/23

Program 10.1 Avoid repeating codeProgram 10.1 Avoid repeating code

Delete the other 2 identical lines of code Delete the other 2 identical lines of code from the other 2 scroll bar scroll event from the other 2 scroll bar scroll event procedures.procedures.Enter the following line in each of the 3 Enter the following line in each of the 3 scroll bar event procedures (to call the scroll bar event procedures (to call the procedure you created on the previous procedure you created on the previous slide):slide): Call ShowFormColour()Call ShowFormColour()

Page 22: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

222203/05/2303/05/23

Program 10.1 Avoid repeating codeProgram 10.1 Avoid repeating code

Run the program and test it.Run the program and test it.

Page 23: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

Commenting on Procedures

In presentations 10.1 – 10.3 I will only ask for comments to procedures.Your comments MUST explain: What is the procedure for? Why and when (after and before what) are you

calling it?

Page 24: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

Extension Program 1Open the program “Student Test Marks” last used in presentation 9.2 Files.

Find the lines which clear the textboxes, labels and list box; and places a cursor into the txtNumber - in the butAdd and butReset procedures.

Place them in a “clear form” procedure and call this procedure instead of repeating the lines.

butAdd & butReset:txtName.Text = ""txtMark.Text = ""txtSearchName.Text = ""lblMark.Text = ""lstDisplayStudentMarks.Items.Clear()txtName.Focus()

Page 25: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

252503/05/2303/05/23

PlenaryPlenary

What is a procedure?What is a procedure? A separate section of code which performs one or A separate section of code which performs one or

more specific tasks and is identified by having a more specific tasks and is identified by having a name. name.

Why are procedures useful?Why are procedures useful? Avoid repeating code.Avoid repeating code. Make the code more readable.Make the code more readable. Help in debugging a program.Help in debugging a program. Use the same procedure in other programs.Use the same procedure in other programs. Pass parameters.Pass parameters.

Page 26: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

262603/05/2303/05/23

PlenaryPlenary

What is the difference between event and What is the difference between event and sub / function procedures?sub / function procedures? Event ProceduresEvent Procedures

Executed in response to events e.g. click, change, Executed in response to events e.g. click, change, ……

Sub / Function proceduresSub / Function proceduresThese are not set off directly by events, but are These are not set off directly by events, but are called by called by code within an event procedure called by called by code within an event procedure or from within another non-event procedure i.e. or from within another non-event procedure i.e. one of the above.one of the above.

Page 27: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

272703/05/2303/05/23

PlenaryPlenary

How do we begin and end a procedure?How do we begin and end a procedure?

Page 28: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

Beginning proceduresBeginning procedures

To begin a procedure use:To begin a procedure use: Private Sub …()Private Sub …()

Procedure NameProcedure Name

PrivatePrivate means that the means that the procedure can only be used procedure can only be used on the form it is declared on on the form it is declared on (all the programs I will show you, (all the programs I will show you, use only one form anyway)use only one form anyway)..

Page 29: 04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures

292903/05/2303/05/23

Ending proceduresEnding procedures

To end a procedure use:To end a procedure use: End SubEnd Sub