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

Slides:



Advertisements
Similar presentations
1 Procedural Programming Paradigm Stacks and Procedures.
Advertisements

Programming Types of Testing.
 Draft timetable has the same times as this semester: - ◦ Monday 9:00 am to 12:00 noon, 1:00 pm to 3:00 pm. ◦ Tuesday 9:00 am to 12:00 noon, 1:00 pm.
Chapter 4 General Procedures
Programming with Objects: Class Libraries and Reusable Code.
VBA Modules, Functions, Variables, and Constants
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Computer Science 1620 Programming & Problem Solving.
Chapter 4 - VB.Net by Schneider1 Chapter 4 General Procedures 4.1 Sub Procedures, Part I 4.2 Sub Procedures, Part II 4.3 Function Procedures 4.4 Modular.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
Chapter 4 Sec. 4.1, 4.2, 4.4 Procedures (User-defined)
The Project AH Computing. Functional Requirements  What the product must do!  Examples attractive welcome screen all options available as clickable.
Python quick start guide
Apply Sub Procedures/Methods and User Defined Functions
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
8.2 Procedures Function Procedures 20/04/2017.
CS0004: Introduction to Programming Variables – Numbers.
1 Shawlands Academy Higher Computing Software Development Unit.
Structured Programming Defn: This is an approach to program development that produces programs of high quality that are easy to test, debug, modify and.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Chapter 5 - VB 2008 by Schneider1 Chapter 5 - General Procedures 5.1 Sub Procedures, Part I 5.2 Sub Procedures, Part II 5.3 Function Procedures 5.4 Modular.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
I Power Higher Computing Software Development The Software Development Process.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (there is an audio component to this eLesson) © Dr.
CPS120: Introduction to Computer Science Functions.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CPS120: Introduction to Computer Science Lecture 14 Functions.
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
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
COPYRIGHT 2010: Dr. David Scanlan, CSUS OBJECTIVES: How to write classes How to create an object from a class using the "New" keyword. How to communicate.
Computing Higher – SD Unit - Topic 8 – Procedure and Standard Algorithms P Lynch, St Andrew’s High School Unit 2 Software Development Process Topic.
The Hashemite University Computer Engineering Department
04/02/ Procedures Top-down approach / Stepwise Refinement & Sub Procedures.
1 The Software Development Process ► Systems analysis ► Systems design ► Implementation ► Testing ► Documentation ► Evaluation ► Maintenance.
Controlling Program Flow with Decision Structures.
Algorithms and Pseudocode
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
OCR A Level F453: The function and purpose of translators Translators a. describe the need for, and use of, translators to convert source code.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
Sub Procedures and Functions Visual Basic. Sub Procedures Slide 2 of 26 Topic & Structure of the lesson Introduction to Modular Design Concepts Write.
Algorithms and Flowcharts
Chapter 9: Value-Returning Functions
User-Written Functions
COMPUTATIONAL CONSTRUCTS
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
Function There are two types of Function User Defined Function
Problem Solving Techniques
Top-down approach / Stepwise Refinement & Sub Procedures
Introduction to Visual Programming
Software Development Process
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
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.
10.2 Procedures Passing Parameters 30/08/2019.
Presentation transcript:

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

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

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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

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

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.

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

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.

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

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.

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.

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

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

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.

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

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:

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

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.

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 Procedures10.2 Procedures Functions: Parameters

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.

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.

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)

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

Plenary What is a procedure?

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.