James Tam Breaking Problems Down This section of notes shows you how to break down a large programming problem into smaller modules that are easier to.

Slides:



Advertisements
Similar presentations
Sub and Function Procedures
Advertisements

Communication between modules, cohesion and coupling
Software Design Deriving a solution which satisfies software requirements.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
Designing Software With Flowcharts And Pseudo-code
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
James Tam Recursion You will learn what is recursion as well as how simple recursive programs work.
James Tam Breaking Problems Down This section of notes shows you how to break down a large programming problem into smaller modules that are easier to.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller parts that are easier to implement and.
James Tam A High-Level Model For Software Development What is involved in the process of designing, writing and maintaining computer programs?
James Tam Arrays In this section of notes you will be introduced to a composite type where all elements must be of the same type (homogeneous): arrays.
Functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 23, 2011) Washington State University.
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
James Tam Functions: Decomposition And Code Reuse (Part I) This section of notes shows you how to write functions that can be used to: decompose large.
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
J. Michael Moore Arrays CSCE 110 From James Tam’s material.
J. Michael Moore Modules: Getting information out CPSC 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Getting Started With Pascal Programming What is the basic structure of a Pascal Program Variables in Pascal Performing input and output with.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
Design The goal is to design a modular solution, using the techniques of: Decomposition Abstraction Encapsulation In Object Oriented Programming this is.
James Tam Breaking Problems Down This section of notes shows you how to break down a large programming problem into smaller modules that are easier to.
J. Michael Moore Software Design CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
J. Michael Moore Software Design CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
James Tam Recursion You will learn what recursion is as well as how simple recursive programs work.
James Tam Breaking Problems Down This section of notes shows you how to break down a large programming problem into smaller modules that are easier to.
CS 201 Functions Debzani Deb.
James Tam Recursion You will learn what is recursion as well as how and when to use it in your programs.
J. Michael Moore Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
Link Lists In this section of notes you will learn how to create and manage a dynamic list. Link Lists in Pascal.
James Tam Introduction To Files In Pascal You will learn how to read from and write to text files in Pascal.
Introduction to Computers and Programming Lecture 13: User defined methods Instructor: Evan Korth New York University.
James Tam Records You will learn in this section of notes what is a record and how to use them in Pascal.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
James Tam Pointers In this section of notes you will learn about a third type of variable that stores addresses rather than data.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
James Tam Introduction To Files In Pascal In this section of notes you will learn how to read from and write to text files in Pascal.
J. Michael Moore Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
Chapter 4 Sec. 4.1, 4.2, 4.4 Procedures (User-defined)
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
James Tam Problem Decomposition This section of notes shows you how to break down a large problem into smaller parts that are easier to implement and manage.
J. Michael Moore Recursion CSCE 110 From James Tam’s material.
Chapter 6: Functions.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
Program Design Simple Program Design Third Edition A Step-by-Step Approach 9.
Subprograms CE 311 K - Introduction to Computer Methods Daene C. McKinney.
CPS120: Introduction to Computer Science 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).
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
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.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Chapter 9: Coupling & Cohesion Omar Meqdadi SE 273 Lecture 9 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Functions: Decomposition And Code Reuse This section of notes shows you how to write functions that can be used to: decompose large problems, and to reduce.
Macro Processor Design Options Recursive Macro Expansion General-Purpose Macro Processors Macro Processing within Language Translators.
History of Computing – Pascal
Coupling and Cohesion Rajni Bhalla.
Pascal Prof. Steven A. Demurjian
Functions: Decomposition And Code Reuse
Arrays In this section of notes you will be introduced to a homogeneous composite type, one-dimensional arrays One-dimensional arrays in Pascal.
Breaking Problems Down
Communication between modules, cohesion and coupling
Computer Science
Presentation transcript:

James Tam Breaking Problems Down This section of notes shows you how to break down a large programming problem into smaller modules that are easier to manage.

James Tam Developing An Algorithm: Top-Down Approach General approach Approach to part of problem Specific steps Approach to part of problem Abstract Particular Top Bottom Figure extracted from Computer Science Illuminated by Dale N. and Lewis J.

James Tam Example Of Top Down Approach Game of Life Initialize biosphere Scan biosphere Scan Neighbours Display board Deal with Existing critter Deliver new critter

James Tam Decomposing Problems (Top Down Approach) Characteristics Breaking problem into smaller, well defined modules Making modules as independent as possible (loose coupling) Benefit Solution is easier to visualize Easier to maintain (if modules are independent) Drawback Complexity – understanding and setting up inter module communication may appear daunting at first Pascal implementation Procedures Functions

James Tam Referring To Functions And Procedures In Pascal Definition Indicating what the function or procedure will do Call Invoking the function or procedure

James Tam Procedures (Basic Case – No Parameters) Procedure call Procedure definition

James Tam Defining Procedures Syntax (Basic case – no parameters): procedure name; begin (* Statements of the function go here *) end; (* End of procedure name *) Example (Basic case – no parameters): procedure displayInstructions; begin writeln ('These statements will typically give a high level'); writeln('overview of what the program as a whole does'); end; (* End of procedure displayInstructions *)

James Tam Calling A Procedure Syntax (Basic case – no parameters): name; Example (Basic case – no parameters): displayInstructions;

James Tam Procedures: Putting Together The Basic Case A compilable version of this example can be found in Unix under /home/231/examples/functions/firstExampleProcedure.p procedure displayInstructions; begin writeln ('These statements will typically give a high level'); writeln('overview of what the program as a whole does'); end; (*Procedure displayInstructions *) begin displayInstructions; writeln('Thank you, come again!'); end. (* Program *)

James Tam Procedures: Putting Together The Basic Case A compilable version of this example can be found in Unix under /home/231/examples/functions/firstExampleProcedure.p procedure displayInstructions; begin writeln ('These statements will typically give a high level'); writeln('overview of what the program as a whole does'); end; (*Procedure displayInstructions *) begin displayInstructions; writeln('Thank you, come again!'); end. (* Program *) Procedure definition Procedure call

James Tam Procedures With Parameters Procedure call P1P2P3 Procedure definition

James Tam Defining Procedures With Parameters Syntax: procedure name (Name of parameter 1 : type of parameter 1; Name of parameter 2 : type of parameter 2; : Name of parameter n : type of parameter n); begin (* Statements of the function go here *) end; Example: procedure celciusToFahrenheit (celciusValue : real); var fahrenheitValue : real; begin fahrenheitValue := 9 / 5 * celciusValue + 32; writeln(temperature in Celsius: ', celciusValue:0:2); writeln(temperature in Fahrenheit: ', fahrenheitValue:0:2); end; (* Procedure celciusToFahrenheit *)

James Tam Calling Procedures With Parameters Syntax: name (Name of parameter 1, Name of parameter 2…Name of parameter n); Example: celciusToFahrenheit(celciusValue);

James Tam Procedures: Putting Together The Case With Parameters A compilable version of this example can be found in Unix under /home/231/examples/functions/temperatureConverter..p program temperatureConverter (input, output); var celciusValue : real; procedure celciusToFahrenheit (celciusValue : real); var fahrenheitValue : real; begin fahrenheitValue := 9 / 5 * celciusValue + 32; writeln('Temperature in Celsius: ', celciusValue:0:2); writeln('Temperature in Fahrenheit: ', fahrenheitValue:0:2); end; (* Procedure celciusToFahrenheit *)

James Tam Procedures: Putting Together The Case With Parameters A compilable version of this example can be found in Unix under /home/231/examples/functions/temperatureConverter.p program temperatureConverter (input, output); var celciusValue : real; procedure celciusToFahrenheit (celciusValue : real); var fahrenheitValue : real; begin fahrenheitValue := 9 / 5 * celciusValue + 32; writeln('Temperature in Celsius: ', celciusValue:0:2); writeln('Temperature in Fahrenheit: ', fahrenheitValue:0:2); end; (* Procedure celciusToFahrenheit *) Procedure definition

James Tam Procedures: Putting Together The Case With Parameters (2) begin writeln; writeln('This program will convert a given temperature from a Celsius's); writeln('value to a Fahrenheit value.'); write('Input temperature in Celsius: '); readln(celciusValue); writeln; celciusToFahrenheit(celciusValue); writeln('Thank you and come again.'); end. (* Program *)

James Tam Procedures: Putting Together The Case With Parameters (2) begin writeln; writeln('This program will convert a given temperature from a Celsius's); writeln('value to a Fahrenheit value.'); write('Input temperature in Celsius: '); readln(celciusValue); writeln; celciusToFahrenheit(celciusValue); writeln('Thank you and come again.'); end. (* Program *) Procedure call

James Tam How Retain Information From A Module After The Module (Function Or Procedure) Has Ended Information needs to be returned from the module i.e., Call Procedure Calculations ???

James Tam How Retain Information From A Module After The Module (Function Or Procedure) Has Ended (2) e.g., producing an income statement program taxes (input, output); var (* Assume declarations made *) begin calculateGrossProfit (grossSales); : : calculateGrossProfit (grossSales: real); var (* Assume declarations made *) begin grossProfit := grossSales – costOfGoodsSold; end; ☼

James Tam How Retain Information From A Module After The Module (Function Or Procedure) Has Ended (3) Methods: Return a value with a function Pass parameters into the procedure as variable parameters (rather than as value parameters)

James Tam Functions Function call P1P2P3 Function definition Return value

James Tam Defining Functions Syntax: function name (Name of parameter 1 : type of parameter 1; Name of parameter 2 : type of parameter 2; : : Name of parameter n : type of parameter n): return type; begin (* Statements of the function go here *) : : name := expression; (* Return value *) end; Example: function calculateGrossIncome (grossSales, costOfGoodsSold : real) : real; begin calculateGrossIncome := grossSales - costOfGoodsSold; end; Should be the last statement in the function

James Tam Calling Functions Syntax: name ; name (name of parameter 1, name of parameter 2…name of parameter n); Example: grossIncome := calculateGrossIncome (grossSales, costOfGoodsSold);

James Tam Functions: Putting It All Together A compilable version of this example can be found in Unix under /home/231/examples/functions/financialStatements.p program financialStatments (input, output); function calculateGrossIncome (grossSales, costOfGoodsSold : real) : real; begin calculateGrossIncome := grossSales - costOfGoodsSold end; function calculateNetIncome (grossIncome, expenses : real) : real; begin calculateNetIncome := grossIncome - expenses; end; Function definitions

James Tam Functions: Putting It All Together (2) procedure produceIncomeStatement; var grossSales : real; costOfGoodsSold : real; grossIncome : real; expenses : real; netIncome : real; begin write('Input gross sales $'); readln(grossSales); write('Input cost of the goods that were sold $'); readln(costOfGoodsSold); write('Input corporate expenses $'); readln(expenses); grossIncome := calculateGrossIncome (grossSales, costOfGoodsSold); netIncome := calculateNetIncome (grossIncome, expenses); Function calls

James Tam Functions: Putting It All Together (3) (* Procedure produceIncomeStatement *) writeln; writeln('Gross sales $':26, grossSales:0:2); writeln('Less: cost of goods sold $':26, costOfGoodsSold:0:2); writeln('Gross income $':26, grossIncome:0:2); writeln('Less: expenses $':26, expenses:0:2); writeln('Net income $':26, netIncome:0:2); writeln; end; (* End of procedure produceIncomeStatement *)

James Tam Functions: Putting It All Together (3) (* Start of program *) begin writeln; writeln('This program will produce an income statement based upon your'); writeln('gross sales figures, the cost of the goods that you sold and writeln('your expenses.'); writeln; produceIncomeStatement; writeln('Thank you, come again!'); end. (* End of entire program. *)

James Tam How Retain Information From A Module After The Module (Function Or Procedure) Has Ended (3) Methods: Return a value with a function Pass parameters into the procedure as variable parameters (rather than as value parameters)

James Tam Passing Parameters As Value Parameters Previous examples procedureName (p1, p2); procedureName (p1, p2: parameter type); begin end;

James Tam Passing Parameters As Value Parameters Previous examples procedureName (p1, p2); procedureName (p1, p2: parameter type); begin end;

James Tam Passing Parameters As Value Parameters Previous examples procedureName (p1, p2); procedureName (p1, p2: parameter type); begin end;

James Tam Passing Parameters As Variable Parameters Example coming up procedureName (p1, p2); procedureName (var p1, p2: parameter type); begin end;

James Tam Passing Parameters As Variable Parameters Example coming up procedureName (p1, p2); procedureName (var p1, p2: parameter type); begin end;

James Tam Procedure Definitions When Passing Parameters As Variable Parameters Syntax: procedure name (var Name of parameter 1 : type of parameter 1; var Name of parameter 2 : type of parameter 2; : var Name of parameter n : type of parameter n); begin (* Statements of the function go here *) end; Example: procedure tabulateIncome ( grossSales : real; costOfGoodsSold : real; var grossIncome : real; expenses : real; var netIncome : real); begin grossIncome := grossSales - costOfGoodsSold; netIncome := grossIncome - expenses; end;

James Tam Calling Procedures With Variable Parameters Same as calling procedures with value parameters! Syntax: name (name of parameter 1, name of parameter 2…name of parameter n); Example: tabulateIncome(grossSales,costOfGoodsSold,grossIncome,expenses, netIncome);

James Tam Passing Variable Parameters: Putting It All Together A compilable version of this example can be found in Unix under /home/231/examples/functions/financialStatements2.p program financialStatments (input, output); procedure getIncomeInformation (var grossSales : real; var costOfGoodsSold : real; var expenses : real); begin write('Input gross sales $'); readln(grossSales); write('Input the cost of the goods that were sold $'); readln(costOfGoodsSold); write('Input business expenses $'); readln(expenses); end; (* End of procedure getIncomeInformation *)

James Tam Passing Variable Parameters: Putting It All Together (2) procedure tabulateIncome ( grossSales : real; costOfGoodsSold : real; var grossIncome : real; expenses : real; var netIncome : real); begin grossIncome := grossSales - costOfGoodsSold; netIncome := grossIncome - expenses; end; (* End of procedure tabulateIncome *) procedure displayIncomeStatement (grossSales : real; costOfGoodsSold : real; grossIncome : real; expenses : real; netIncome : real);

James Tam Passing Variable Parameters: Putting It All Together (3) (* Procedure displayIncomeStatement *) begin writeln; writeln('INCOME STATEMENT':40); writeln('Gross sales $':40, grossSales:0:2); writeln('Less: Cost of the goods that were sold $':40, costOfGoodsSold:0:2); writeln('Equals: Gross Income $':40, grossIncome:0:2); writeln('Less: Business Operating Expenses $':40, expenses:0:2); writeln('Equals: Net income $':40, netIncome:0:2); writeln; end; (* End of procedure displayIncomeStatement *) procedure produceIncomeStatement; var grossSales, grossIncome, costOfGoodsSold, expenses, netIncome :real;

James Tam Passing Variable Parameters: Putting It All Together (3) begin getIncomeInformation(grossSales, costOfGoodsSold, expenses); tabulateIncome(grossSales,costOfGoodsSold,grossIncome,expenses,netIncome); displayIncomeStatement (grossSales,costOfGoodsSold,grossIncome,expenses,netIncome); end; (* Begin main program *) begin writeln; writeln('This program will produce an income statement based upon your'); writeln('gross sales figures, the cost of the goods that you sold and'); writeln('your expenses.'); writeln; produceIncomeStatement; writeln('Thank you, come again!'); end. (* End of main program *)

James Tam Summary How to break up a Pascal program into modules using functions and procedures What is the difference between a procedure/function definition and a call as well as how to write each one in Pascal with the different cases: No parameters Parameters passed in as variable parameters Parameters passed in as value parameters