03/05/2303/05/23 11
10.1 Procedures10.1 ProceduresTop-down approach / Stepwise Top-down approach / Stepwise
Refinement Refinement & Sub Procedures& 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.
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.
4403/05/2303/05/23
How do we find the area of a 'house' made up from a square and
a triangle?
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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)..
161603/05/2303/05/23
Ending proceduresEnding procedures
To end a procedure use:To end a procedure use: End SubEnd Sub
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.
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.
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.
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.
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()
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.
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?
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()
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.
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.
272703/05/2303/05/23
PlenaryPlenary
How do we begin and end a procedure?How do we begin and end a procedure?
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)..
292903/05/2303/05/23
Ending proceduresEnding procedures
To end a procedure use:To end a procedure use: End SubEnd Sub