Upload
annabel-daniels
View
220
Download
0
Tags:
Embed Size (px)
Citation preview
Top-down approach / Top-down approach / Stepwise Refinement Stepwise Refinement
& & Procedures & FunctionsProcedures & Functions
Learning Objectives:Learning Objectives:
Give the advantages and disadvantages of the top down approach / stepwise refinement.
Define procedures and functions.
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.
4421/04/2321/04/23
How do we find the area of a 'house' made up from a square and
a triangle?
5521/04/2321/04/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.
6621/04/2321/04/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.
7721/04/2321/04/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.
8821/04/2321/04/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 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.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.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 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.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.
101021/04/2321/04/23
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 proceduresSub procedures
Function procedures.Function 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.
111121/04/2321/04/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 from a software library used from a software library in other programsin other programs..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.
Variables may clash across modules.Variables may clash across modules.
Parameters may be of the wrong type.Parameters may be of the wrong type.
Documentation of modules must be Documentation of modules must be thorough.thorough.
141421/04/2321/04/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 As with naming controls and variables, no spaces are allowed.spaces 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)..
161621/04/2321/04/23
Ending proceduresEnding procedures
To end a procedure use:To end a procedure use: End SubEnd Sub
171721/04/2321/04/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.
VariablesVariables
When a program runs, the results to any When a program runs, the results to any calculations it performs are stored in RAM.calculations it performs are stored in RAM.The results to these calculations will be The results to these calculations will be lost as soon as the computer starts doing lost as soon as the computer starts doing something else.something else.A variable is a name made up by a A variable is a name made up by a programmer to reserve and identify an programmer to reserve and identify an address in RAM where data can be stored address in RAM where data can be stored as long as the program runs.as long as the program runs.
Scope of a variableScope of a variable
Location declared Level Description
Within a procedure. Local scope
Available only within the module in which it is declared
Outside a procedure.
Global scope
Available to all modules.
Narrow scopeNarrow scopeIt is good programming practice and efficient to It is good programming practice and efficient to declare variables with as narrow a scope as possible.declare variables with as narrow a scope as possible.Certain errors/problems can occur if you don’t:Certain errors/problems can occur if you don’t: Name Conflict Avoidance Name Conflict Avoidance – several different procedures – several different procedures
can use the same variable name/identifier. As long as each can use the same variable name/identifier. As long as each instance is declared as a (local) procedure variable, each instance is declared as a (local) procedure variable, each procedure recognizes only its own version of the variable. procedure recognizes only its own version of the variable.
Memory Consumption Memory Consumption – (Local) Procedure variables – (Local) Procedure variables consume memory only while their procedure is running. consume memory only while their procedure is running. Their memory is released when the procedure finishes. Their memory is released when the procedure finishes. (Global) Module variables obviously use memory until the (Global) Module variables obviously use memory until the module finishes i.e. longer.module finishes i.e. longer.
212121/04/2321/04/23
How do we find the area of a 'house' made up from a square and
a triangle?
Input TriangleHeight Input TriangleHeight
Input TriangleBaseInput TriangleBase
Input RectangleWidth Input RectangleWidth
Input RectangleLengthInput RectangleLength
CalcTriangleAreaCalcTriangleArea
CalcRectangleAreaCalcRectangleArea
CalcHouseAreaCalcHouseArea
Output HouseAreaOutput HouseArea
HouseArea = HouseArea = AreaTriangle + AreaTriangle + AreaRectangle AreaRectangle
AreaTriangle = ½ * AreaTriangle = ½ * TriangleHeight TriangleHeight * TriangleBase* TriangleBase
AreaRectangle = AreaRectangle = RectangleWidth * RectangleWidth * RectangleBreadthRectangleBreadth
TriangleHeightTriangleHeightTriangleHeightTriangleHeight
TriangleBaseTriangleBaseTriangleBaseTriangleBase
RectangleWidthRectangleWidthRectangleLengthRectangleLength
CalcTriangleAreaCalcTriangleAreaCalcTriangleAreaCalcTriangleArea
TriangleAreaTriangleAreaTriangleAreaTriangleArea
CalcAreaRectangleCalcAreaRectangleCalcAreaRectangleCalcAreaRectangle
RectangleAreaRectangleArea
CalcHouseAreaCalcHouseAreaCalcHouseAreaCalcHouseAreaAreaTriangleAreaTriangle
AreaRectangleAreaRectangle
HouseAreaHouseArea
Main ProgramMain ProgramMain ProgramMain Program
TriangleAreaTriangleAreaTriangleAreaTriangleArea
RectangleAreaRectangleArea
HouseAreaHouseArea
Note that the variable to be returned e.g. TriangleArea also has to be sent to the sub procedure so Note that the variable to be returned e.g. TriangleArea also has to be sent to the sub procedure so its value can be changed by the sub procedure - see its value can be changed by the sub procedure - see Value and Reference parameters – later. – later.
Sub Procedures:Sub Procedures:
ParametersParameters – – see the next few slides for details.see the next few slides for details.
ParametersParameters
Variables passed between (sent to and / Variables passed between (sent to and / or from) modules.or from) modules. Necessary if the called procedure needs a Necessary if the called procedure needs a
local variable from the calling procedure.local variable from the calling procedure.
242421/04/2321/04/23
ParametersParametersVariables (with stored data) Variables (with stored data) Sent Sent toto the called procedure from the calling the called procedure from the calling
procedure.procedure.OrOr Sent Sent fromfrom the called procedure to the calling the called procedure to the calling
procedure after it has finished.procedure after it has finished.
Needed only when:Needed only when: The called procedure requires data to perform its The called procedure requires data to perform its
task.task.Or Or The calling procedure requires data which the called The calling procedure requires data which the called
procedure produces.procedure produces.
PassingPassing = sending to or from = sending to or from
252521/04/2321/04/23
Actual Actual andand Formal Formal ParametersParameters
Actual ParametersActual Parameters Variables that are Variables that are passedpassed to a procedure when it is to a procedure when it is
called.called. i.e. Call …(…, …, …)i.e. Call …(…, …, …)
Formal ParametersFormal Parameters Variables which must be Variables which must be declareddeclared in the procedure in the procedure
which must match in number, position and data type which must match in number, position and data type the actual parameters sent to it.the actual parameters sent to it.
i.e. Private Sub …(…, …, …)i.e. Private Sub …(…, …, …)
262621/04/2321/04/23
FormalFormal Parameters Parameters
You can use the same identifiers / names You can use the same identifiers / names as the actual parameters they match.as the actual parameters they match.
It is optional whether you declare the data It is optional whether you declare the data types of formal parameters types of formal parameters (as they must be (as they must be the same as the actual parameters they match so are the same as the actual parameters they match so are
inherited from them)inherited from them).. However, your code is clearer if you do.However, your code is clearer if you do.
My programs will follow this suggestion.My programs will follow this suggestion.
272721/04/2321/04/23
Value and Reference ParametersValue and Reference Parameters
Formal Parameters can be declared as:Formal Parameters can be declared as: Value Parameters (ByVal)Value Parameters (ByVal)
Parameters in a called procedure which are Parameters in a called procedure which are notnot passed passed back to the calling procedure. back to the calling procedure.
Reference Parameters (ByRef)Reference Parameters (ByRef)
Parameters in a called procedure which Parameters in a called procedure which areare passed back to passed back to the calling procedure.the calling procedure.
i.e. Ones which have been changed by the called procedure i.e. Ones which have been changed by the called procedure and these changes are required by the calling procedure.and these changes are required by the calling procedure.
i.e. Private Sub …(ByVal … As …, ByRef… As …)i.e. Private Sub …(ByVal … As …, ByRef… As …)
Input TriangleHeight Input TriangleHeight
Input TriangleBaseInput TriangleBase
Input RectangleWidth Input RectangleWidth
Input RectangleLengthInput RectangleLength
CalcTriangleAreaCalcTriangleArea
CalcRectangleAreaCalcRectangleArea
CalcHouseAreaCalcHouseArea
Output HouseAreaOutput HouseArea
HouseArea = HouseArea = AreaTriangle + AreaTriangle + AreaRectangle AreaRectangle
AreaTriangle = ½ * AreaTriangle = ½ * TriangleHeight TriangleHeight * TriangleBase* TriangleBase
AreaRectangle = AreaRectangle = RectangleWidth * RectangleWidth * RectangleBreadthRectangleBreadth
TriangleHeightTriangleHeightTriangleHeightTriangleHeight
TriangleBaseTriangleBaseTriangleBaseTriangleBase
RectangleWidthRectangleWidthRectangleLengthRectangleLength
CalcTriangleAreaCalcTriangleAreaCalcTriangleAreaCalcTriangleArea
TriangleAreaTriangleAreaTriangleAreaTriangleArea
CalcAreaRectangleCalcAreaRectangleCalcAreaRectangleCalcAreaRectangle
RectangleAreaRectangleArea
CalcHouseAreaCalcHouseAreaCalcHouseAreaCalcHouseAreaAreaTriangleAreaTriangle
AreaRectangleAreaRectangle
HouseAreaHouseArea
Main ProgramMain ProgramMain ProgramMain Program
TriangleAreaTriangleAreaTriangleAreaTriangleArea
RectangleAreaRectangleArea
HouseAreaHouseArea
Can you work out which of the parameters below should be passed by Can you work out which of the parameters below should be passed by ValueValue and which should be passed by and which should be passed by ReferenceReference??
(See the next slide for the answers!)(See the next slide for the answers!)Sub Procedures:Sub Procedures:
Input TriangleHeight Input TriangleHeight
Input TriangleBaseInput TriangleBase
Input RectangleWidth Input RectangleWidth
Input RectangleLengthInput RectangleLength
CalcTriangleAreaCalcTriangleArea
CalcRectangleAreaCalcRectangleArea
CalcHouseAreaCalcHouseArea
Output HouseAreaOutput HouseArea
HouseArea = HouseArea = AreaTriangle + AreaTriangle + AreaRectangle AreaRectangle
AreaTriangle = ½ * AreaTriangle = ½ * TriangleHeight TriangleHeight * TriangleBase* TriangleBase
AreaRectangle = AreaRectangle = RectangleWidth * RectangleWidth * RectangleBreadthRectangleBreadth
TriangleHeightTriangleHeightTriangleHeightTriangleHeight
TriangleBaseTriangleBaseTriangleBaseTriangleBase
RectangleWidthRectangleWidthRectangleLengthRectangleLength
CalcTriangleAreaCalcTriangleAreaCalcTriangleAreaCalcTriangleArea
TriangleAreaTriangleAreaTriangleAreaTriangleArea
CalcAreaRectangleCalcAreaRectangleCalcAreaRectangleCalcAreaRectangle
RectangleAreaRectangleArea
CalcHouseAreaCalcHouseAreaCalcHouseAreaCalcHouseAreaAreaTriangleAreaTriangleAreaRectangleAreaRectangle
HouseAreaHouseArea
Main ProgramMain ProgramMain ProgramMain Program
TriangleAreaTriangleAreaTriangleAreaTriangleArea
RectangleAreaRectangleArea
HouseAreaHouseArea
Sub Procedures:Sub Procedures:
KeyKey::
……....
ReferenceReference parameterparameter
……....
ValueValue parameterparameter
KeyKey::
……....
ReferenceReference parameterparameter
……....
ValueValue parameterparameter
3030
Function ProceduresFunction ProceduresKnown as just Known as just functionsfunctions for short for short (sub procedures (sub procedures are known as just are known as just proceduresprocedures for short). for short).
Always returns one item of data to the calling Always returns one item of data to the calling procedure.procedure. Remember that:Remember that:
Sub procedures:Sub procedures: May return one or more items of data or no data at all to the calling May return one or more items of data or no data at all to the calling
procedure.procedure.
If only If only one value is to be returned one value is to be returned then athen a Function Function is more is more suitablesuitable, , if if two or more values (or no values) two or more values (or no values) need to be returned then a procedure is more suitableneed to be returned then a procedure is more suitable ..
313121/04/2321/04/23
Built-in & User-defined FunctionsBuilt-in & User-defined Functions
Built-in FunctionsBuilt-in Functions Supplied by VB, Supplied by VB, some of which you have used previously e.g. some of which you have used previously e.g.
Format, Mid, TodayFormat, Mid, Today
User-defined FunctionsUser-defined FunctionsWritten by the programmer.Written by the programmer.
You will learn to write these here.You will learn to write these here.
Input TriangleHeight Input TriangleHeight
Input TriangleBaseInput TriangleBase
Input RectangleWidth Input RectangleWidth
Input RectangleLengthInput RectangleLength
CalcTriangleAreaCalcTriangleArea
CalcRectangleAreaCalcRectangleArea
CalcHouseAreaCalcHouseArea
Output HouseAreaOutput HouseArea
HouseArea = HouseArea = AreaTriangle + AreaTriangle + AreaRectangle AreaRectangle
AreaTriangle = ½ * AreaTriangle = ½ * TriangleHeight TriangleHeight * TriangleBase* TriangleBase
AreaRectangle = AreaRectangle = RectangleWidth * RectangleWidth * RectangleBreadthRectangleBreadth
TriangleHeightTriangleHeightTriangleHeightTriangleHeight
TriangleBaseTriangleBaseTriangleBaseTriangleBase
RectangleWidthRectangleWidthRectangleLengthRectangleLength
CalcTriangleAreaCalcTriangleAreaCalcTriangleAreaCalcTriangleArea
TriangleAreaTriangleAreaTriangleAreaTriangleArea
CalcAreaRectangleCalcAreaRectangleCalcAreaRectangleCalcAreaRectangle
RectangleAreaRectangleArea
CalcHouseAreaCalcHouseAreaCalcHouseAreaCalcHouseAreaAreaTriangleAreaTriangle
AreaRectangleAreaRectangle
HouseAreaHouseArea
Main ProgramMain ProgramMain ProgramMain Program
Note that the variable to be returned no longer has to be sent to Function procedures (as they do to Note that the variable to be returned no longer has to be sent to Function procedures (as they do to Sub Procedures – see Sub Procedures – see 10.2 Procedures), so all formal parameters are value parameters. A ), so all formal parameters are value parameters. A functionfunction just returns just returns oneone answer and it is left up to the main program to “know” what that answer means. answer and it is left up to the main program to “know” what that answer means.
FunctionsFunctions::
ParametersParameters
333321/04/2321/04/23
Writing a FunctionWriting a Function
Similar to Sub procedures.Similar to Sub procedures.Differences:Differences: Its job is to return only one item of data.Its job is to return only one item of data. So all formal parameters are value parameters So all formal parameters are value parameters
(see the last slide)(see the last slide)..i.e. Declared with i.e. Declared with ByValByVal
Starts with Private Starts with Private FunctionFunction instead of instead of SubSub.. The first line of the function (after the Private The first line of the function (after the Private
Function…) has to declare the variable which Function…) has to declare the variable which will be returned:will be returned:
DimDim (Variable Name to be returned) (Variable Name to be returned) As As (Data Type)(Data Type)
Continued on the next slide.Continued on the next slide.
343421/04/2321/04/23
Writing a FunctionWriting a Function
Similar to Sub procedures.Similar to Sub procedures.Differences:Differences: Its job is to return only one item of data.Its job is to return only one item of data. So all formal parameters are value parameters.So all formal parameters are value parameters.
i.e. Declared with i.e. Declared with ByValByVal Starts with Private Starts with Private FunctionFunction instead of instead of SubSub.. The first line of the function (after the Private The first line of the function (after the Private
Function…) has to declare the variable which Function…) has to declare the variable which will be returned:will be returned:
DimDim (Variable Name to be returned) (Variable Name to be returned) As As (Data Type)(Data Type)
Continued on the next slide.Continued on the next slide.
353521/04/2321/04/23
Writing a FunctionWriting a Function
The last line of the function (before the End The last line of the function (before the End Function) will return the one item of data to the Function) will return the one item of data to the calling procedure:calling procedure:
ReturnReturn (Variable name to be returned) (Variable name to be returned) When the function is called you treat it as a When the function is called you treat it as a
value (i.e. the value it returns).value (i.e. the value it returns).Store it in a variable:Store it in a variable:
VariableNameVariableName = FunctionName( = FunctionName(Variables to be passedVariables to be passed))
Display it directly:Display it directly: ControlControl.Text = FunctionName(.Text = FunctionName(Variables to be passedVariables to be passed))
363621/04/2321/04/23
Writing a FunctionWriting a Function
i.e.i.e. Private Function …(ByVal … As …, Private Function …(ByVal … As …,
By Val… As …)By Val… As …)DimDim (Variable name to be returned) (Variable name to be returned) As As (Data Type)(Data Type)
……
Return (Variable name to be returned) Return (Variable name to be returned) End FunctionEnd Function
PlenaryPlenary
What is a procedure?What is a procedure?
ProceduresProcedures
Modules in program code: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.Does a defined task. Is executed when called by its name / Is executed when called by its name /
identifier.identifier.