Presentation is loading. Please wait.

Presentation is loading. Please wait.

Top-down approach / Stepwise Refinement & Procedures & Functions.

Similar presentations


Presentation on theme: "Top-down approach / Stepwise Refinement & Procedures & Functions."— Presentation transcript:

1 Top-down approach / Stepwise Refinement & Procedures & Functions

2 Learning Objectives: Give the advantages and disadvantages of the top down approach / stepwise refinement. Define procedures and functions.

3 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.

4 424/12/2015 How do we find the area of a 'house' made up from a square and a triangle?

5 524/12/2015 Prose (language description) Find the area of the triangle by multiplying the base by the height and halving. Find the area of the rectangle by multiplying the width by the breadth. Then add the area of the triangle and rectangle together. Each sentence is a module. Each sentence is a module.

6 624/12/2015 Formulae BT = Base of the triangle BT = Base of the triangle HT = Height of the triangle HT = Height of the triangle W = Width of rectangle W = Width of rectangle BR = Breadth of rectangle BR = Breadth of rectangle AT = Area of the triangle AT = Area of the triangle AR = Area of the rectangle AR = Area of the rectangle AT = ½ BH AR = W + H + W + H = 2 * (H + W) Area of the house = AT + AR Each formula is a module.

7 724/12/2015 Ordered Steps 1.Find the height and base of the triangle. 2.Find the area of the triangle by multiplying the height of the triangle by the base of the triangle, and halving. 3.Find the width and breadth of the rectangle. 4.Find the area of the rectangle by adding the width of the rectangle to the breadth of the rectangle, and multiplying by 2. 5.Add the areas of the triangle and rectangle together. Each step is a module.

8 824/12/2015Flowchart Ordered steps using arrows to point from one instruction to the next instead of numbers. Find the height and base of the triangle. Find the width and breadth of the rectangle. Add the areas of the triangle and rectangle together. Find the area of the triangle by multiplying the height of the triangle by the base of the triangle, and halving. Find the area of the rectangle by adding the width of the rectangle to the breadth of the rectangle, and multiplying by 2.

9 Using procedures is the top-down approach to programming Procedures are modules in program code: A small subprogram which is given a name / identifier. A small subprogram which is given a name / identifier. Does a defined task or combination of related defined tasks. Does a defined task or combination of related defined tasks. Is identified by having a name and is executed when called by its name / identifier. Is identified by having a name and is executed when called by its name / identifier.

10 1024/12/2015 Main Types of Procedure in VB Event 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 procedures Function procedures. These are not set off directly by events, but are called by code within an event procedure or from within another non-event procedure i.e. one of the above. These are not set off directly by events, but are called by code within an event procedure or from within another non-event procedure i.e. one of the above.

11 1124/12/2015 Why use procedures? Obviously most programs inevitably use event procedures. You don’t have to use sub or function procedures but there are several advantages: See the next slide. See the next slide.

12 Top-Down Approach / Stepwise Refinement - Advantages Avoids repeating code as modules can be stored in and used from a software library in other programs. Makes the code more readable. By splitting a problem / code into smaller parts the solution is easier to follow. By splitting a problem / code into smaller parts the solution is easier to follow. Helps in debugging a program. Fewer errors are likely to be made. Any errors that are made will be easier to correct. Many people can be involved in the solution. Individual skills can be used. The last 2 bold advantages should be used if an exam question scenario involves more than one person. The last 2 bold advantages should be used if an exam question scenario involves more than one person.

13 Top-Down Approach / Stepwise Refinement - Disadvantages Individual modules may work as required but they may be linked incorrectly, so the links must be thoroughly tested. Variables may clash across modules. Parameters may be of the wrong type. Documentation of modules must be thorough.

14 1424/12/2015 Writing and calling procedures Sub and function procedures should be written outside event procedures i.e. Where you declare global variables. i.e. Where you declare global variables. i.e. Click below the line: Public Class Form1 Public Class Form1 Press enter to make a blank line if necessary. As with naming controls and variables, no spaces are allowed. We will use the same convention as for naming variables: We will use the same convention as for naming variables: i.e. Each word starts with a capital Also remember to always use meaningful names. Also remember to always use meaningful names.

15 Beginning procedures To begin a procedure use: Private Sub …() Private Sub …() Procedure Name Private means that the procedure can only be used on the form it is declared on (all the programs I will show you, use only one form anyway).

16 1624/12/2015 Ending procedures To end a procedure use: End Sub End Sub

17 1724/12/2015 Calling procedures To call a procedure use: Call … Call … Procedure Name You don’t actually have to use Call. You can just the procedure’s name. However, your code is more readable if you do and it makes it easier to differentiate between a procedure and a variable identifier / name.

18 Variables When a program runs, the results to any calculations it performs are stored in RAM. The results to these calculations will be lost as soon as the computer starts doing something else. A variable is a name made up by a programmer to reserve and identify an address in RAM where data can be stored as long as the program runs.

19 Scope of a variable Location declaredLevelDescription Within a procedure. Local scope Available only within the module in which it is declared Outside a procedure. Global scope Available to all modules.

20 Narrow scope It is good programming practice and efficient to declare variables with as narrow a scope as possible. Certain errors/problems can occur if you don’t: Name Conflict Avoidance – several different procedures can use the same variable name/identifier. As long as each instance is declared as a (local) procedure variable, each procedure recognizes only its own version of the variable. Name Conflict Avoidance – several different procedures can use the same variable name/identifier. As long as each instance is declared as a (local) procedure variable, each procedure recognizes only its own version of the variable. Memory Consumption – (Local) Procedure variables consume memory only while their procedure is running. Their memory is released when the procedure finishes. (Global) Module variables obviously use memory until the module finishes i.e. longer. Memory Consumption – (Local) Procedure variables consume memory only while their procedure is running. Their memory is released when the procedure finishes. (Global) Module variables obviously use memory until the module finishes i.e. longer.

21 2124/12/2015 How do we find the area of a 'house' made up from a square and a triangle?

22 Input TriangleHeight Input TriangleBase Input RectangleWidth Input RectangleLength CalcTriangleAreaCalcRectangleAreaCalcHouseArea Output HouseArea HouseArea = AreaTriangle + AreaRectangle AreaTriangle = ½ * TriangleHeight * TriangleBase AreaRectangle = RectangleWidth * RectangleBreadth TriangleHeightTriangleHeight TriangleBaseTriangleBase RectangleWidth RectangleLength CalcTriangleAreaCalcTriangleArea TriangleAreaTriangleArea CalcAreaRectangleCalcAreaRectangle RectangleArea CalcHouseAreaCalcHouseArea AreaTriangle AreaRectangle HouseArea Main Program TriangleAreaTriangleArea RectangleArea HouseArea 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 Value and Reference parameters – later. Value and Reference parametersValue and Reference parameters Sub Procedures: Parameters – see the next few slides for details.

23 Parameters Variables passed between (sent to and / or from) modules. Necessary if the called procedure needs a local variable from the calling procedure. Necessary if the called procedure needs a local variable from the calling procedure.

24 2424/12/2015 Parameters Variables (with stored data) Sent to the called procedure from the calling procedure. Sent to the called procedure from the calling procedure.Or Sent from the called procedure to the calling procedure after it has finished. Sent from the called procedure to the calling procedure after it has finished. Needed only when: The called procedure requires data to perform its task. The called procedure requires data to perform its task.Or The calling procedure requires data which the called procedure produces. The calling procedure requires data which the called procedure produces. Passing = sending to or from Passing = sending to or from

25 2524/12/2015 Actual and Formal Parameters Actual Parameters Variables that are passed to a procedure when it is called. Variables that are passed to a procedure when it is called. i.e. Call …(…, …, …) i.e. Call …(…, …, …) Formal Parameters Variables which must be declared in the procedure which must match in number, position and data type the actual parameters sent to it. Variables which must be declared in the procedure which must match in number, position and data type the actual parameters sent to it. i.e. Private Sub …(…, …, …) i.e. Private Sub …(…, …, …)

26 2624/12/2015 Formal Parameters You can use the same identifiers / names as the actual parameters they match. It is optional whether you declare the data types of formal parameters (as they must be the same as the actual parameters they match so are 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.

27 2724/12/2015 Value and Reference Parameters Formal Parameters can be declared as: Value Parameters (ByVal) Value Parameters (ByVal) Parameters in a called procedure which are not passed back to the calling procedure. Reference Parameters (ByRef) Reference Parameters (ByRef) Parameters in a called procedure which are passed back to the calling procedure. i.e. Ones which have been changed by the called procedure and these changes are required by the calling procedure. i.e. Ones which have been changed by the called 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 …)

28 Input TriangleHeight Input TriangleBase Input RectangleWidth Input RectangleLength CalcTriangleAreaCalcRectangleAreaCalcHouseArea Output HouseArea HouseArea = AreaTriangle + AreaRectangle AreaTriangle = ½ * TriangleHeight * TriangleBase AreaRectangle = RectangleWidth * RectangleBreadth TriangleHeightTriangleHeight TriangleBaseTriangleBase RectangleWidth RectangleLength CalcTriangleAreaCalcTriangleArea TriangleAreaTriangleArea CalcAreaRectangleCalcAreaRectangle RectangleArea CalcHouseAreaCalcHouseArea AreaTriangle AreaRectangle HouseArea Main Program TriangleAreaTriangleArea RectangleArea HouseArea Can you work out which of the parameters below should be passed by Value and which should be passed by Reference? (See the next slide for the answers!) (See the next slide for the answers!) Sub Procedures:

29 Input TriangleHeight Input TriangleBase Input RectangleWidth Input RectangleLength CalcTriangleAreaCalcRectangleAreaCalcHouseArea Output HouseArea HouseArea = AreaTriangle + AreaRectangle AreaTriangle = ½ * TriangleHeight * TriangleBase AreaRectangle = RectangleWidth * RectangleBreadth TriangleHeightTriangleHeight TriangleBaseTriangleBase RectangleWidth RectangleLength CalcTriangleAreaCalcTriangleArea TriangleAreaTriangleArea CalcAreaRectangleCalcAreaRectangle RectangleArea CalcHouseAreaCalcHouseArea AreaTriangle AreaRectangle HouseArea Main Program TriangleAreaTriangleArea RectangleArea HouseArea Sub Procedures: Key: ….. Reference parameter ….. Value parameter Key: ….. Reference parameter ….. Value parameter

30 30 Function Procedures Known as just functions for short (sub procedures are known as just procedures for short). Always returns one item of data to the calling procedure. Remember that: Remember that: Sub procedures: May return one or more items of data or no data at all to the calling procedure. May return one or more items of data or no data at all to the calling procedure. If only one value is to be returned then a Function is more suitable, if two or more values (or no values) need to be returned then a procedure is more suitable.

31 3124/12/2015 Built-in & User-defined Functions Built-in Functions Supplied by VB, Supplied by VB, some of which you have used previously e.g. Format, Mid, Today some of which you have used previously e.g. Format, Mid, Today User-defined Functions Written by the programmer. You will learn to write these here.

32 Input TriangleHeight Input TriangleBase Input RectangleWidth Input RectangleLength CalcTriangleAreaCalcRectangleAreaCalcHouseArea Output HouseArea HouseArea = AreaTriangle + AreaRectangle AreaTriangle = ½ * TriangleHeight * TriangleBase AreaRectangle = RectangleWidth * RectangleBreadth TriangleHeightTriangleHeight TriangleBaseTriangleBase RectangleWidth RectangleLength CalcTriangleAreaCalcTriangleArea TriangleAreaTriangleArea CalcAreaRectangleCalcAreaRectangle RectangleArea CalcHouseAreaCalcHouseArea AreaTriangle AreaRectangle HouseArea Main Program Note that the variable to be returned no longer has to be sent to Function procedures (as they do to Sub Procedures – see 10.2 Procedures), so all formal parameters are value parameters. A function just returns one answer and it is left up to the main program to “know” what that answer means. 10.2 Procedures10.2 Procedures Functions: Parameters

33 3324/12/2015 Writing a Function Similar to Sub procedures. 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 (see the last slide). So all formal parameters are value parameters (see the last slide). i.e. Declared with ByVal Starts with Private Function instead of Sub. Starts with Private Function instead of Sub. The first line of the function (after the Private Function…) has to declare the variable which will be returned: The first line of the function (after the Private Function…) has to declare the variable which will be returned: Dim (Variable Name to be returned) As (Data Type) Continued on the next slide.

34 3424/12/2015 Writing a Function Similar to Sub procedures. 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 ByVal Starts with Private Function instead of Sub. Starts with Private Function instead of Sub. The first line of the function (after the Private Function…) has to declare the variable which will be returned: The first line of the function (after the Private Function…) has to declare the variable which will be returned: Dim (Variable Name to be returned) As (Data Type) Continued on the next slide.

35 3524/12/2015 Writing a Function The last line of the function (before the End Function) will return the one item of data to the calling procedure: The last line of the function (before the End Function) will return the one item of data to the calling procedure: Return (Variable name to be returned) When the function is called you treat it as a value (i.e. the value it returns). When the function is called you treat it as a value (i.e. the value it returns). Store it in a variable: VariableName = FunctionName(Variables to be passed) VariableName = FunctionName(Variables to be passed) Display it directly: Control.Text = FunctionName(Variables to be passed) Control.Text = FunctionName(Variables to be passed)

36 3624/12/2015 Writing a Function i.e. Private Function …(ByVal … As …, By Val… As …) Private Function …(ByVal … As …, By Val… As …) Dim (Variable name to be returned) As (Data Type) … Return (Variable name to be returned) End Function End Function

37 Plenary What is a procedure?

38 Procedures Modules in program code: A small subprogram which is given a name / identifier. A small subprogram which is given a name / identifier. Does a defined task. Does a defined task. Is executed when called by its name / identifier. Is executed when called by its name / identifier.


Download ppt "Top-down approach / Stepwise Refinement & Procedures & Functions."

Similar presentations


Ads by Google