Structured Programming

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Week 4 – Functions Introduction. Functions: Purpose Breaking a large problem into a series of smaller problems is a common problem- solving technique.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
ITEC113 Algorithms and Programming Techniques
Computer Science 1620 Loops.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
The Project AH Computing. Functional Requirements  What the product must do!  Examples attractive welcome screen all options available as clickable.
Python quick start guide
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Structured Programming Defn: This is an approach to program development that produces programs of high quality that are easy to test, debug, modify and.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
1 MT258 Computer Programming and Problem Solving Unit 7.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Higher Grade Computing Studies 3. High Level Language Constructs Higher Computing Software Development S. McCrossan 1 Simple Data Types Integer: An integer.
Programming, an introduction to Pascal
Cosc175/module.ppt1 Introduction to Modularity Function/procedures void/value-returning Arguments/parameters Formal arguments/actual arguments Pass by.
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
Pascal Programming Today Chapter 11 1 Chapter 11.
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.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Programming Fundamentals Enumerations and Functions.
A First Book of ANSI C Fourth Edition
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
15.1 More About High-Level Programming Languages Syntax and Semantics Each programming language contains its own rules for both syntax and semantics. Syntax.
C++ LANGUAGE MULTIPLE CHOICE QUESTION
Development Environment
Chapter 9: Value-Returning Functions
ALGORITHMS AND FLOWCHARTS
Structured Programming
Topics Introduction to Functions Defining and Calling a Void Function
Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Functions II
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Value-Returning Functions
UNIT - V STORED PROCEDURE.
The Selection Structure
Algorithm development
Lecture 2 Introduction to Programming
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Microsoft Access Illustrated
User-Defined Functions
Control Statement Examples
Designing and Debugging Batch and Interactive COBOL Programs
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Chapter 4 - Visual Basic Schneider
Computer Science and an introduction to Pascal
Unit# 9: Computer Program Development
Problem Solving Techniques
Chapter 4 void Functions
Programming Logic and Design Fourth Edition, Comprehensive
Procedures Brent M. Dingle Texas A&M University
Chapter 7: User-Defined Functions II
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
In C Programming Language
CodePainter Revolution Trainer Course
Basic Concepts of Algorithm
Algorithms, Part 3 of 3 Topics Top down-design Structure charts
Algorithms, Part 3 of 3 Topics Top down-design Reading
Presentation transcript:

Structured Programming Defn: This is an approach to program development that produces programs of high quality that are easy to test, debug, modify and maintain. This technique emphasizes the modular approach to program development. Modular Programming: Using this approach a large complex problem is broken up into sub-problems, and if necessary these are further divided until the tasks to be solved are simpler or easier for programming code to be developed. – Top Down Design ADD A MEMBER SEARCH FOR A MEMBER DELETE A MEMBER

Structured Programming Defn: A structure chart is ideally a map of your program which shows how variables pass data between modules in a program. It helps the programmer to produce a top-down design of the program Steps: Place the main module as the root of an upside-down tree Determine the set of tasks necessary for solving the problem. Place these below the root and connect the root to each task Break down each task into even smaller tasks if necessary. Eventually the program is broken down to a point where the “leaves” of the structure chart “tree” represent simple modules that can be coded with just a few statements.

Structure Charts Automatic Teller Activate Customer Accounts Handle transactions Validate Customer Pull customer record Check Transactions Savings Transactions Exit Transactions Get Valid Card # Get Valid Code # Handle Queries Handle Deposits Handle withdrawals Fig 2. A structure chart,. Each box (node) in the chart represents a block of code called a module. The root node (box at the top) represents the main section of the program. Each connecting line represents a call to a module. The connecting lines could be variables (data) which are passed to and from modules.

Structured Programming Most of the programs written at the ordinary and advanced level courses are likely to be short; however, in the IT arena most problems will require thousands, if not millions of lines of code. (Windows 2000 – over 35 millions lines of code). The importance of splitting a problem into a series of self-contained modules then becomes obvious. A module should not exceed 100 lines, and preferably short enough to fit on a single page or screen. ADVANTAGES OF MODULAR PROGRAMMING Allows a problem to be split in stages and for a team of programmers to work on the problem, also enabling a faster solution. Senior programmers can be given the more complex modules to write, and the junior programmers the simpler ones. Program modification is easier since changes can be isolated to specific modules. Modules can be tested and debugged independently. Since a module performs a specific well defined task, it is possible to develop and place commonly used modules in a user library so that they can be accessed by different programs. (e.g. Validation, Uppercase, Text color…Pascal) If a programmer cannot continue through the entire project, it is easier for another programmer to continue working on self contained modules.

Structured Programming In the Pascal programming language the concept of structured programming is expressed using Procedures and Functions. Procedures are portions of code that perform a specific task such as draw a square on screen, prompt a user to enter values, display results on screen etc. They can exist as independent statements e.g. textcolor(red), inc(x), clrscr; Functions are also portions of code that perform a specific task. However, they return a value or result, such as an integer, a real number, a string , etc. Consequently, they can only be used as components of statements e.g. ch:=upcase (ch); y:= abs(z); Consider the following simple example. Program Example1: Procedure open_screen; Begin writeln(‘ Welcome to Cyber Core….’); end; open_screen; End. Program header Procedure header End of Procedure Start of main program Calling the procedure open_screen End of program

Structured Programming Examine the Pascal program below which prompts a user for two names and outputs the one that is alphabetically smaller. The program uses a procedure which creates a border (of 20 asterisks) before and after the output. Note also that the procedure is called twice from the main program and that procedures and functions are written before the main program. Program smaller_name; Var name1,name2:string; Procedure Border; Var i:integer; Begin for i:= 1 to 20 do write(‘*’); writeln; End; writeln(‘ Enter two names ‘):readln(name1,name2); Border; If (name1<name2) then writeln(name1) else writeln(name2); End. Program header Global Variables Local Variable End of Procedure Start of main program Call Procedure Call Procedure again

Structured Programming Notice that the procedure will only print 20 asterisks, no more and no less. If 10 or even 40 asterisks are required then the same procedure can be used, though some modification will be required. This can be achieved with the use of a parameter list with one variable, say Num_asterisks . The procedure now looks as follows: Procedure Border(Num_asterisks:integer); Var i:integer; Begin for i:= 1 to Num_asterisks do write(‘*’); writeln; End; On examining the new format the number of asterisks is passed to the procedure through the parameter list in the procedure header. Consequently, when the procedure needs to be called, the value of the parameter is placed in brackets. So calls in the main program would look like Border(10); or Border(40) ; for printing 10 or 40 asterisks respectively. Global variables exist throughout the scope of a program. They are declared at the start of the program and values may be accessed at any time and position during the execution of the program. Local Variables are declared within a procedure or function. They are created when the subroutine is started and only exist when that portion of code is executed. When the procedure or function is exited the memory space which was reserved for the local variables is released for other use. N.B to minimize the constraints for the running of a program more local declarations should be made where possible. Parameter List

Structured Programming In the Pascal programming language Functions are very similar to Procedures except that they return a value. The golden rule is that if a value is not to be returned then a procedure should always be used. When programming the reserved word Function is used instead of the word Procedure and the data type of the returning value must be stated. Format of a Pascal Function Function Name _of _function (parameter_listing) :datatype Var any_local_variables; Begin statement1; statement2; Name_of_Function:= value; End; Function is_even (num : integer):boolean; if (num mod 2 = 0) then is_even:=true else is_even:= false; end Integer, string, char, boolean Must be given a value consistent with the data type stated

Structured Programming Consider the Pascal program below and answer the questions that follow. Program test1; Var ans, num:integer; Procedure cubed(num: integer; var ans:integer); Begin ans:= num * num * num; End; Writeln(‘Enter number : ‘); readln (num); cubed(num , ans); Writeln(‘Number Cubed : ‘,ans); readln; End. What is the name of the program? Write the names of the global and local variables. State the parameter list in the procedure. For each parameter , identify whether it is a variable or value parameter and why. What will be the output if the following numbers are entered (a) 4 (b) 3.2 ? Do you think cubed should be written as a Function? If so why? Assume you decide to write cube as a Function, rewrite the above portion of code. Program test1; Var num:integer; Function Cubed(num: integer) :integer; cubed:=num * num * num; Begin writeln (‘Enter number : ‘); readln (num); writeln(‘Number Cubed : ‘, cubed(num));

Structured Programming Consider writing a program where a user is required to enter integer values M and N and to calculate and output M raised to the power N. In analyzing the problem, three cases need to be considered: the case of N being Zero the case of N being positive and finally the case of N being negative The modular breakdown of the problem now looks as follows: Power Raising M + n M -n M0 It is now possible to focus on one module at a time and to develop the internal workings (stepwise refinement). For the Zero case the body of the module would need only contain the statement setting the answer to 1 (Any number raised to the power of Zero equals 1!) For the Positive Case the following portion of code could be used ans:= 1; for i:= 1 to N do ans:= ans * M For the Negative Case it is observed that M – n is equal to that of 1/ M + n. Thus, use could be made of previously written code for the positive case. (all that is necessary is for the procedure to be appropriately called)