Presentation is loading. Please wait.

Presentation is loading. Please wait.

04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures.

Similar presentations


Presentation on theme: "04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures."— Presentation transcript:

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

2 204/02/2016 Learning Objectives Define a procedure. Explain why procedures are useful. Explain the difference between event and sub / function procedures. State how to begin and end a procedure.

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 404/02/2016 How do we find the area of a 'house' made up from a square and a triangle?

5 504/02/2016 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 604/02/2016 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 704/02/2016 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 804/02/2016Flowchart 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 10 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 (sometimes shortened to just procedures). Function procedures (sometimes shortened to just functions and these will be looked at in detail in presentation 10.3 Procedures). 10.3 Procedures10.3 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 1104/02/2016 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 in other programs from a software library. 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. Documentation of modules must be thorough. Variables may clash across modules. Parameters may be of the wrong type. The last 2 disadvantages will become clear once you have covered presentation 10.2 Procedures. The last 2 disadvantages will become clear once you have covered presentation 10.2 Procedures.10.2 Procedures10.2 Procedures

14 1404/02/2016 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 1604/02/2016 Ending procedures To end a procedure use: End Sub End Sub

17 1704/02/2016 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 1804/02/2016 Program 10.1 Avoid repeating code Specification: Illustrate how procedures can make it unnecessary to repeat code in two or more procedures. Illustrate how procedures can make it unnecessary to repeat code in two or more procedures.

19 1904/02/2016 Program 10.1 Avoid repeating code Open the “Colour Change” Program you wrote in 2.3 Working with controls. 2.3 Working with controls2.3 Working with controls Make a copy of the previous program’s whole folder to keep the original program but rename this folder with the same name but add (Procedure Version). Make a copy of the previous program’s whole folder to keep the original program but rename this folder with the same name but add (Procedure Version). Each of the 3 scroll bars has a single line of identical code in their scroll events. We will write this code only once in a procedure.

20 2004/02/2016 Program 10.1 Avoid repeating code Drag one of the lines of identical code outside its event procedure. i.e. where you would declare global variables. i.e. where you would declare global variables. i.e. Below the line: Public Class Form1 Public Class Form1 Press enter to make a blank line if necessary. Enter the following line before it: Private Sub ShowFormColour() ‘Declare the Sub procedure. Private Sub ShowFormColour() ‘Declare the Sub procedure. Enter the following line after it: End Sub ‘End the Sub procedure. End Sub ‘End the Sub procedure.

21 2104/02/2016 Program 10.1 Avoid repeating code Delete the other 2 identical lines of code from the other 2 scroll bar scroll event procedures. Enter the following line in each of the 3 scroll bar event procedures (to call the procedure you created on the previous slide): Call ShowFormColour() Call ShowFormColour()

22 2204/02/2016 Program 10.1 Avoid repeating code Run the program and test it.

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

24 Extension Program 1 Open the program “Student Test Marks” last used in presentation 9.2 Files.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()

25 2504/02/2016 Plenary What is a procedure? A separate section of code which performs one or more specific tasks and is identified by having a name. A separate section of code which performs one or more specific tasks and is identified by having a name. 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.

26 2604/02/2016 Plenary What is the difference between event and sub / function procedures? Event Procedures Event Procedures Executed in response to events e.g. click, change, … Sub / Function procedures Sub / Function procedures These are not set off directly by events, but are called by called by code within an event procedure or from within another non-event procedure i.e. one of the above.

27 2704/02/2016 Plenary How do we begin and end a procedure?

28 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).

29 2904/02/2016 Ending procedures To end a procedure use: End Sub End Sub


Download ppt "04/02/20161 10.1 Procedures Top-down approach / Stepwise Refinement & Sub Procedures."

Similar presentations


Ads by Google