J. Michael Moore Modules: Getting information out CPSC 110 Influenced by material developed by James Tam & Jennifer Welch.

Slides:



Advertisements
Similar presentations
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Advertisements

J. Michael Moore Text Files CSCE 110 From James Tam’s material.
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 Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
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 Pointers In this section of notes you will learn about another type of variable that stores addresses rather than data.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Introduction To Files In Pascal In this section of notes you will learn how to read from and write to files in Pascal.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
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.
James Tam Introduction To Files In Pascal In this section of notes you will learn how to read from and write to files in your Pascal programs.
J. Michael Moore Scope & Testing Modules CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
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 From James Tam’s material Multi-Dimensional Arrays CSCE 110.
J. Michael Moore Structured Programming CPSC 110 Drawn from James Tam's material.
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.
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
J. Michael Moore Arrays CSCE 110 From James Tam’s material.
James Tam Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables and constants.
James Tam Getting Started With Pascal Programming What is the basic structure of a Pascal Program Variables in Pascal Performing input and output with.
J. Michael Moore Structured Programming CSCE 110 Drawn from James Tam's material.
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 Input and Output (IO) CSCE 110 Drawn from James Tam's material.
J. Michael Moore From James Tam’s material Multi-Dimensional Arrays CSCE 110.
J. Michael Moore Software Design CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
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.
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
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 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.
James Tam Introduction To Files In Pascal You will learn how to read from and write to text files in Pascal.
J. Michael Moore Arrays CSCE 110. J. Michael Moore Typical (although simplified) Problem Write a program that will track student grades in a class. The.
James Tam Records You will learn in this section of notes what is a record and how to use them in Pascal.
James Tam Pointers In this section of notes you will learn about another type of variable that stores addresses rather than data.
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.
Input and Output (IO) CSCE 110 Drawn from James Tam's material.
James Tam Pointers In this section of notes you will learn about a third type of variable that stores addresses rather than data.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
J. Michael Moore Scope & Testing Modules CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
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 files in Pascal.
James Tam Pointers In this section of notes you will learn about another type of variable that stores addresses rather than data.
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.
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.
J. Michael Moore Recursion CSCE 110 From James Tam’s material.
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.
Structured Programming Defn: This is an approach to program development that produces programs of high quality that are easy to test, debug, modify and.
Lesson 1: Introduction to ABAP OBJECTS Todd A. Boyle, Ph.D. St. Francis Xavier University.
CPS120: Introduction to Computer Science Functions.
Introduction to Pascal The Basics of Program writing.
Programming, an introduction to Pascal
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
JavaScript, Fourth Edition
Pascal Programming Today Chapter 11 1 Chapter 11.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
1 More on Readln:numerical values Note: ENTER key counts sends a carriage return and a line feed to the computer definition: “white space”: space, tab,
Scion Macros How to make macros for Scion The Fast and Easy Guide.
Input and Output Output Prompt: –A message displayed to the user indicating what they are to enter. Should be clear and exact on what you want. write:
History of Computing – Pascal
Structured Programming
Principles of programming languages 4: Parameter passing, Scope rules
Pascal Prof. Steven A. Demurjian
Structured Programming
Anatomy of a Function Part 3
Pascal Subprogram Procedure Function Build in Function (e.g. Sin(x))
Breaking Problems Down
Presentation transcript:

J. Michael Moore Modules: Getting information out CPSC 110 Influenced by material developed by James Tam & Jennifer Welch

J. Michael Moore By default, Pascal passes parameters by value. So how do we get information out of a module? program taxes (input, output); begin (* Assume declarations are made *) calculateGrossProfit (grossSales, costOfGoodSold); : : end. calculateGrossProfit (grossSales : real; costOfGoodsSold : real); var grossProfit : real; begin grossProfit := grossSales – costOfGoodsSold; end; ☼ grossProfit How?

J. Michael Moore Retaining Information From A Module Methods: Return a value with a function Pass parameters into the procedure as variable parameters (pass by reference instead of by value)

J. Michael Moore Functions Function call P1P1 P2P2 …P n Function definition Returns exactly one value

J. Michael Moore Defining Functions Format: 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 : real; costOfGoodsSold : real) : real; begin calculateGrossIncome := grossSales - costOfGoodsSold; end;

J. Michael Moore Calling Functions Format: variable := name of function; variable := name of function (name of parameter 1, name of parameter 2…name of parameter n); Example: grossIncome := calculateGrossIncome (grossSales, costOfGoodsSold); Where else can we call a function?

J. Michael Moore Tracing The Execution Of A Function Call procedure produceIncomeStatement; var grossSales: real; costOfGoodsSold: real; begin grossIncome := calculateGrossIncome (grossSales, costOfGoodsSold); function calculateGrossIncome (grossSales: real; costOfGoodsSold: real) : real; begin calculateGrossIncome := grossSales - costOfGoodsSold end;

J. Michael Moore Example Problem Write a program that will produce a simple income statement: Gross sales Cost of goods sold Gross income Expenses Net income The values for gross sales, cost of goods sold and expenses will be entered by the user. Gross income and net income will be calculated by the program.

J. Michael Moore Functions: Putting It All Together financialStatements.pas program financialStatements (input, output); function calculateGrossIncome (grossSales: real; costOfGoodsSold: real) : real; begin calculateGrossIncome := grossSales - costOfGoodsSold end; function calculateNetIncome (grossIncome : real; expenses : real) : real; begin calculateNetIncome := grossIncome - expenses; end;

J. Michael Moore Functions: Putting It All Together (2) procedure produceIncomeStatement; var grossSales: real; costOfGoodsSold: real; grossIncome: real; expenses: real; netIncome: real; begin write(‘Enter gross sales $'); readln(grossSales); write(‘Enter cost of the goods that were sold $'); readln(costOfGoodsSold); write(‘Enter corporate expenses $'); readln(expenses); grossIncome := calculateGrossIncome (grossSales, costOfGoodsSold); netIncome := calculateNetIncome (grossIncome, expenses);

J. Michael Moore Functions: Putting It All Together (3) (* Procedure produceIncomeStatement continued *) 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 *)

J. Michael Moore Functions: Putting It All Together (4) procedure intro; 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; end;

J. Michael Moore Functions: Putting It All Together (5) (* Start of main program *) begin intro; produceIncomeStatement; writeln('Thank you, come again!'); end. (* End of entire program. *)

J. Michael Moore Retaining Information From A Module Methods: Return a value with a function Pass parameters into the procedure as variable parameters (pass by reference instead of by value)

J. Michael Moore Passing Parameters By Value Previous examples procedureName (p1); procedureName (p1 : parameter type); begin end;

J. Michael Moore Passing Parameters by Value Previous examples procedureName (p1); procedureName (p1 : parameter type); begin end; Pass a copy

J. Michael Moore Passing Parameters by Reference (As Variable Parameters) Example coming up procedureName (p1); procedureName (var p1 : parameter type); begin end;

J. Michael Moore Passing Parameters by Reference (As Variable Parameters) Example coming up procedureName (p1); procedureName (var p1 : parameter type); begin end; Pass the variable In Pascal we often say: Pass by Variable Pass as a var parameter

J. Michael Moore Procedure Definitions When Passing Parameters As Variable Parameters Format: 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 procedure 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;

J. Michael Moore Calling Procedures With Variable Parameters It’s the same as calling procedures with value parameters! Format: name (name of parameter 1, name of parameter 2…name of parameter n); Example: tabulateIncome(grossSales,costOfGoodsSold,grossIncome,expenses, netIncome); Almost

J. Michael Moore Limitations on Actual Parameters Formal parameter is passed by value Actual Parameter can be —Variable of the same type —Constant of the same type —Literal of the same type —Function call that returns a value of the same type —Expression that results in a value of the same type Formal parameter is passed by reference i.e. var paramenter Actual Parameter MUST be a variable of the same type

J. Michael Moore Variable Parameters: A Simple Example program variableExample (output); procedure valueProc (num : integer); begin num := num * 10; end; procedure varProc (var num : integer); begin num := num * 2; end; begin var num : integer; num := 5; writeln (num); valueProc (num); writeln (num); varProc (num); writeln (num); end.

J. Michael Moore Passing Variable Parameters: Putting Together A Solution To A Real Problem financialStatements2.pas program financialStatements (input, output); procedure getIncomeInformation (var grossSales: real; var costOfGoodsSold: real; var expenses: real); begin write(‘Enter gross sales $'); readln(grossSales); write(‘Enter the cost of the goods that were sold $'); readln(costOfGoodsSold); write(‘Enter business expenses $'); readln(expenses); end; (* End of procedure getIncomeInformation *)

J. Michael Moore 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 *)

J. Michael Moore Passing Variable Parameters: Putting It All Together (3) procedure displayIncomeStatement (grossSales: real; costOfGoodsSold: real; grossIncome: real; expenses: real; netIncome: real); 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 displayIncomeStatement *)

J. Michael Moore Passing Variable Parameters: Putting It All Together (4) procedure produceIncomeStatement; var grossSales: real; costOfGoodsSold: real; grossIncome: real; expenses: real; netIncome: real; begin getIncomeInformation(grossSales, costOfGoodsSold, expenses); tabulateIncome(grossSales,costOfGoodsSold,grossIncome,expenses,netIncome); displayIncomeStatement (grossSales,costOfGoodsSold,grossIncome,expenses,netIncome); end; (* End of procedure produceIncomeStatement *)

J. Michael Moore Passing Variable Parameters: Putting It All Together (5) procedure intro; 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; end;

J. Michael Moore Passing Variable Parameters: Putting It All Together (6) (* Begin main program *) begin intro; produceIncomeStatement; writeln('Thank you, come again!'); end. (* End of main program *)

J. Michael Moore Functions: Exactly one value is returned by the function. Variable parameters: One or more parameters may be modified in the module Functions Vs. Variable Parameters function calculateGrossIncome (grossSales : real; costOfGoodsSold : real) : real; begin calculateGrossIncome := grossSales - costOfGoodsSold; end; procedure tabulateIncome ( grossSales : real; costOfGoodsSold : real; var grossIncome : real; expenses : real; var netIncome : real); begin grossIncome := grossSales - costOfGoodsSold; netIncome := grossIncome - expenses; end;

J. Michael Moore Where To Declare Variables? main introproduceIncomeStatement Local variables grossSales costOfGoodsSold grossIncome expenses netIncome getIncomeInformation tabulateIncome displayIncomeStatement grossSales costOfGoodsSold expenses grossSales costOfGoodsSold grossIncome expenses netIncome grossSales costOfGoodsSold grossIncome expenses netIncome Declare variables at the ‘lowest’ possible level

J. Michael Moore Where To Declare Variables: Example A toy program will consist of 3 modules (excluding main): Prompt the user to enter a number Display the number Caller of the previous two modules main getAndDisplay getInput display The number is only needed in this part of the program. getInput Num?

J. Michael Moore Where To Declare Variables: Example A toy program will consist of 3 modules (excluding main): Prompt the user to enter a number Display the number Caller of the previous two modules main getAndDisplay getInput display getInput Num? Num is needed here In order to pass num into both modules, this module must be familiar with the variable. Works if num is declared here. It’s not directly needed by modules it calls. So, it’s declared at an unnecessarily high level i.e. it's a global variable. and here.. Declaring it in either place makes it local to each module.

J. Michael Moore Where To Declare Variables: Solution To The Example program designExample (input, output); procedure getInput (var num : integer); begin write ('Enter number: '); readln (num); end; procedure display (num : integer); begin writeln ('Num=', num); end; procedure getAndDisplay; var num : integer; begin getInput (num); display (num); end; begin getAndDisplay; end.

J. Michael Moore Variable Parameters And Multiple Module Calls If during a series of module calls, a parameter must be passed as a variable parameter during one of the later module calls then the earlier module calls must also pass the parameter as a variable parameter. procedure proc1 (var num : integer); begin num := num * 10; end; In proc1 ‘num’ is being changed so if those modifications are to persist after the module ends then num must be passed as variable parameter. procedure proc2 (var num : integer); begin proc1(num); end; If the changes made to ‘num’ in proc1 are to persist after proc2 ends then num must ALSO be passed as a variable parameter to proc2

J. Michael Moore Variable Parameters And Multiple Module Calls (3) program parameter1 (output); procedure proc1 (var num : integer); begin num := num * 10; end; procedure proc2 (num : integer); begin writeln('num=', num); proc1(num); end; begin var num : integer; num := 7; writeln('num=', num); proc2(num); writeln('num=', num); end. The changes made to ‘num’ in proc1 will be made to a variable that is local to proc2. The variable num that is local to ‘main’ will not be modified by the changes made in proc1.

J. Michael Moore Variable Parameters And Multiple Module Calls (2) program parameter1 (output); procedure proc1 (var num : integer); begin num := num * 10; end; procedure proc2 (var num : integer); begin writeln('num=', num); proc1(num); end; begin var num : integer; num := 7; writeln('num=', num); proc2(num); writeln('num=', num); end. If the changes made to ‘num’ in proc1 are to persist after the procedure calls end then num must ALSO be passed as a variable parameter to proc2