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

Slides:



Advertisements
Similar presentations
30/04/ Selection Nested If structures & Complex Multiple Conditions.
Advertisements

Programming Logic and Design Fourth Edition, Introductory
Programming Logic and Design, Third Edition Comprehensive
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Copyright © 2001 by Wiley. All rights reserved. Chapter 1: Introduction to Programming and Visual Basic Computer Operations What is Programming? OOED Programming.
The Project AH Computing. Functional Requirements  What the product must do!  Examples attractive welcome screen all options available as clickable.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
Bret Juliano. Introduction What Documentation is Required? – To use a program – To believe a program – To modify a program The Flow-Chart Curse Self-Documenting.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
08/09/ Arrays Defining, Declaring & Processing.
1 Shawlands Academy Higher Computing Software Development Unit.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Learning Objectives Data and Information Six Basic Operations Computer Operations Programs and Programming What is Programming? Types of Languages Levels.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
PMS /134/182 HEX 0886B6 PMS /39/80 HEX 5E2750 PMS /168/180 HEX 00A8B4 PMS /190/40 HEX 66CC33 By Adrian Gardener Date 9 July 2012.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Return To Index Excel Microsoft Excel Basics Lesson 10 Adding Information To Worksheets Adding Information To Worksheets - 2 Format Cells (Cell.
CHAPTER 13 Creating a Workbook Part 1. Learning Objectives Understand spreadsheets and Excel Enter data in cells Edit cell content Work with columns and.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
1 The Software Development Process  Systems analysis  Systems design  Implementation  Testing  Documentation  Evaluation  Maintenance.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works.
Just as there are many human languages, there are many computer programming languages that can be used to develop software. Some are named after people,
Describe the Program Development Cycle. Program Development Cycle The program development cycle is a series of steps programmers use to build computer.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
30/10/ Iteration Loops Do While (condition is true) … Loop.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (there is an audio component to this eLesson) © Dr.
B065: PROGRAMMING Sub Procedures I. Starter  Identify the separate tasks to be performed in the programming task below (break it down into numbered sections).
I Power Higher Computing Software Development Development Languages and Environments.
The Software Development Process
CIS 234: Project 1 Issues Dr. Ralph D. Westfall April, 2010.
Chapter One An Introduction to Programming and Visual Basic.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Files Tutor: You will need ….
Top-down approach / Stepwise Refinement & Procedures & Functions.
Computing Higher – SD Unit - Topic 8 – Procedure and Standard Algorithms P Lynch, St Andrew’s High School Unit 2 Software Development Process Topic.
Introduction to OOP CPS235: Introduction.
The Hashemite University Computer Engineering Department
31/01/ Selection If selection construct.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 3 Building an Application in the Visual Basic.NET Environment.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
CSCI 161 Lecture 3 Martin van Bommel. Operating System Program that acts as interface to other software and the underlying hardware Operating System Utilities.
Evolution of C and C++ n C was developed by Dennis Ritchie at Bell Labs (early 1970s) as a systems programming language n C later evolved into a general-purpose.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
Sub Procedures and Functions Visual Basic. Sub Procedures Slide 2 of 26 Topic & Structure of the lesson Introduction to Modular Design Concepts Write.
Software Development Languages and Environments. Computer Languages Just as there are many human languages, there are many computer programming languages.
Creating a Workbook Part 1
Visit for more Learning Resources
3.1 Fundamentals of algorithms
Exploring Mathematical Relationships Module 5: Investigation 1
MODULAR PROGRAMMING Many programs are too large to be developed by one person. programs are routinely developed by teams of programmers The linker program.
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
Lecture 2 Introduction to Programming
Using Procedures and Exception Handling
Problem Solving Techniques
Visual Basic.
Top-down approach / Stepwise Refinement & Sub Procedures
CIS16 Application Development Programming with Visual Basic
Do … Loop Until (condition is true)
Software Development Process
10.3 Procedures Function Procedures 07/06/2019.
Top-down technique / Stepwise Refinement and Algorithms
3.2 Working with Data Scope of variables 29/07/2019.
8 Records 25/07/2019.
10.2 Procedures Passing Parameters 30/08/2019.
Presentation transcript:

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

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.

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.

404/02/2016 How do we find the area of a 'house' made up from a square and a triangle?

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.

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.

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.

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.

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

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.

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.

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

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.

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

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

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.

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.

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.

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.

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()

2204/02/2016 Program 10.1 Avoid repeating code 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 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()

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.

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.

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

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

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