Topdown approach Stepwise Refinement Procedures Functions Learning Objectives

Top-down approach / Stepwise Refinement & Procedures & Functions

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

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

How do we find the area of a 'house' made up from a square and a triangle? 07/01/2022 4

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. n 07/01/2022 Each sentence is a module. 5

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

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. 07/01/2022 Each step is a module. 7

Flowchart 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 area of the triangle by multiplying the height of the triangle by the base of the triangle, and halving. Find the width and breadth of the rectangle. Find the area of the rectangle by adding the width of the rectangle to the breadth of the rectangle, and multiplying by 2. Add the areas of the triangle and rectangle together. 07/01/2022 8

Using procedures is the top-down approach to programming Procedures are modules in program code: n n n A small subprogram which is given a name / identifier. 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.

Main Types of Procedure in VB Event procedures n n These are the ones you have used so far. Executed in response to events e. g. click, change, … Sub procedures Function procedures. n 1/7/2022 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. 10

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: n 1/7/2022 See the next slide. 11

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. n 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. n The last 2 bold advantages should be used if an exam question scenario involves more than one person.

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.

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

Beginning procedures To begin a procedure use: n 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).

Ending procedures To end a procedure use: n 1/7/2022 End Sub 16

Calling procedures To call a procedure use: n 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. 1/7/2022 17

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.

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

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: n n 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.

How do we find the area of a 'house' made up from a square and a triangle? 07/01/2022 21

Sub Procedures: Main Program Input Triangle. Height Input Triangle. Base Input Rectangle. Width Input Rectangle. Length Calc. Triangle. Area Calc. Rectangle. Area Calc. House. Area Output House. Area Parameters – see the next few slides for details. Triangle. Height Triangle. Base Triangle. Area Calc. Triangle. Area. Triangle = ½ * Triangle. Height * Triangle. Base Rectangle. Length Rectangle. Width Rectangle. Area Calc. Area. Rectangle = Rectangle. Width * Rectangle. Area Rectangle. Breadth House. Area. Rectangle Area. Triangle House. Area Calc. House. Area = Area. Triangle + Area. Rectangle Note that the variable to be returned e. g. Triangle. Area 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.

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

Parameters Variables (with stored data) n Sent to the called procedure from the calling procedure. Or n 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. Or n The calling procedure requires data which the called procedure produces. n 1/7/2022 Passing = sending to or from 24

Actual and Formal Parameters Actual Parameters n n Variables that are passed to a procedure when it is called. i. e. Call …(…, …, …) Formal Parameters n n 1/7/2022 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 …(…, …, …) 25

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). n However, your code is clearer if you do. My programs will follow this suggestion. 1/7/2022 26

Value and Reference Parameters Formal Parameters can be declared as: n Value Parameters (By. Val) Parameters in a called procedure which are not passed back to the calling procedure. n Reference Parameters (By. Ref) Parameters in a called procedure which are passed back to the calling procedure. n n 1/7/2022 i. e. Ones which have been changed by the called procedure and these changes are required by the calling procedure. i. e. Private Sub …(By. Val … As …, By. Ref… As …) 27

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!) Sub Procedures: Main Program Input Triangle. Height Input Triangle. Base Input Rectangle. Width Input Rectangle. Length Calc. Triangle. Area Calc. Rectangle. Area Calc. House. Area Output House. Area Triangle. Height Triangle. Base Triangle. Area Calc. Triangle. Area. Triangle = ½ * Triangle. Height * Triangle. Base Rectangle. Length Rectangle. Width Rectangle. Area Calc. Area. Rectangle = Rectangle. Width * Rectangle. Area Rectangle. Breadth House. Area. Rectangle Area. Triangle House. Area Calc. House. Area = Area. Triangle + Area. Rectangle

Key: …. . Value parameter …. . Reference parameter Main Program Input Triangle. Height Input Triangle. Base Input Rectangle. Width Input Rectangle. Length Calc. Triangle. Area Calc. Rectangle. Area Calc. House. Area Output House. Area Triangle. Height Triangle. Base Triangle. Area Sub Procedures: Calc. Triangle. Area. Triangle = ½ * Triangle. Height * Triangle. Base Rectangle. Length Rectangle. Width Rectangle. Area Calc. Area. Rectangle = Rectangle. Width * Rectangle. Area Rectangle. Breadth Area. Rectangle Area. Triangle House. Area Calc. House. Area = Area. Triangle + Area. Rectangle

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. n Remember that: Sub procedures: n 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 30 is more suitable.

Built-in & User-defined Functions Built-in Functions l l Supplied by VB, 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. 1/7/2022 31

Functions: Main Program Input Triangle. Height Input Triangle. Base Input Rectangle. Width Input Rectangle. Length Calc. Triangle. Area Calc. Rectangle. Area Calc. House. Area Output House. Area Parameters Triangle. Height Triangle. Base Triangle. Area Calc. Triangle. Area. Triangle = ½ * Triangle. Height * Triangle. Base Calc. Area. Rectangle. Length Rectangle. Width Area. Rectangle = Rectangle. Width * Rectangle. Area Rectangle. Breadth Calc. House. Area. Triangle House. Area = Area. Triangle + House. Area. Rectangle 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.

Writing a Function Similar to Sub procedures. Differences: n n Its job is to return only one item of data. So all formal parameters are value parameters (see the last slide). i. e. Declared with By. Val n n 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: Dim (Variable Name to be returned) As (Data Type) 1/7/2022 Continued on the next slide. 33

Writing a Function Similar to Sub procedures. Differences: n n Its job is to return only one item of data. So all formal parameters are value parameters. i. e. Declared with By. Val n n 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: Dim (Variable Name to be returned) As (Data Type) 1/7/2022 Continued on the next slide. 34

Writing a Function n 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) n When the function is called you treat it as a value (i. e. the value it returns). Store it in a variable: n Variable. Name = Function. Name(Variables to be passed) Display it directly: n 1/7/2022 Control. Text = Function. Name(Variables to be passed) 35

Writing a Function i. e. n Private Function …(By. Val … As …, By Val… As …) Dim (Variable name to be returned) As (Data Type) … Return (Variable name to be returned) n 1/7/2022 End Function 36

Plenary What is a procedure?

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