DOC PREVIEW
TAMU CSCE 110 - 11-modules-info-out
Type Miscellaneous
Pages 36

This preview shows page 1-2-17-18-19-35-36 out of 36 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

J. Michael MooreModules:Getting information outCPSC 110Influenced by material developed by James Tam & Jennifer WelchJ. Michael MooreBy 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);vargrossProfit : real;begingrossProfit := grossSales – costOfGoodsSold;end;☼grossProfitHow?J. Michael MooreRetaining Information From A ModuleMethods:• Return a value with a _________• Pass parameters into the procedure as __________parameters (pass by ________ instead of by _______)J. Michael MooreFunctionsFunction callP1P2…PnFunction definitionReturns _________valueJ. Michael MooreDefining FunctionsFormat: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;begincalculateGrossIncome := grossSales - costOfGoodsSold;end;J. Michael MooreCalling FunctionsFormat: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 MooreTracing The Execution Of A Function Callprocedure produceIncomeStatement;vargrossSales : real;costOfGoodsSold : real;begingrossIncome := calculateGrossIncome (grossSales, costOfGoodsSold);function calculateGrossIncome (grossSales : real;costOfGoodsSold: real) : real;begincalculateGrossIncome := grossSales - costOfGoodsSoldend;J. Michael MooreExample 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 MooreFunctions: Putting It All TogetherfinancialStatements.pasprogram financialStatements (input, output);function calculateGrossIncome (grossSales : real;costOfGoodsSold : real) : real;begincalculateGrossIncome := grossSales - costOfGoodsSoldend; function calculateNetIncome (grossIncome : real;expenses : real) : real;begincalculateNetIncome := grossIncome - expenses;end;J. Michael MooreFunctions: Putting It All Together (2)procedure produceIncomeStatement;vargrossSales : real;costOfGoodsSold : real;grossIncome : real;expenses : real;netIncome : real;beginwrite(‘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 MooreFunctions: 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 MooreFunctions: Putting It All Together (4)procedure intro;beginwriteln;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 MooreFunctions: Putting It All Together (5)(* Start of main program *)beginintro;produceIncomeStatement;writeln('Thank you, come again!');end. (* End of entire program. *)J. Michael MooreRetaining Information From A ModuleMethods:• Return a value with a ______________• Pass parameters into the procedure as _____________parameters (pass by __________ instead of by _________)J. Michael MoorePassing Parameters By ________Previous examplesprocedureName (p1);procedureName (p1 : parameter type);beginend;J. Michael MoorePassing Parameters by _______Previous examplesprocedureName (p1);procedureName (p1 : parameter type);beginend;Pass a ____J. Michael MoorePassing Parameters by ___________(As ___________ Parameters)Example coming upprocedureName (p1);procedureName (var p1 : parameter type);beginend;J. Michael MoorePassing Parameters by ___________(As ___________ Parameters)Example coming upprocedureName (p1);procedureName (var p1 : parameter type);beginend;Pass the __________In Pascal we often say:• Pass by ___________• Pass as a _____________J. Michael MooreProcedure Definitions When Passing Parameters As ____________ParametersFormat: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);begingrossIncome := grossSales - costOfGoodsSold;netIncome := grossIncome - expenses;end;J. Michael MooreCalling Procedures With __________ ParametersIt’s the same as calling procedures with _______ parameters!Format:name (name of parameter 1, name of parameter 2…name of parameter n);Example:tabulateIncome(grossSales,costOfGoodsSold,grossIncome,expenses,netIncome);AlmostJ. Michael MooreLimitations on Actual Parameters• Formal parameter is passed by ________• Actual Parameter can be—__________________________ of the same type—__________________________ of the same type—__________________________ of the same type—__________________________ of the same type—__________________________ of the same type• Formal parameter is passed by __________i.e. _________________• Actual Parameter MUST be _______________of the same typeJ. Michael MooreVariable Parameters: A Simple Exampleprogram variableExample (output);procedure valueProc (num : integer);beginnum := num * 10;end;procedure varProc (var num : integer);beginnum := num * 2;end;beginvar num : integer;num := 5;writeln


View Full Document

TAMU CSCE 110 - 11-modules-info-out

Type: Miscellaneous
Pages: 36
Documents in this Course
06-IO

06-IO

29 pages

21-OOP

21-OOP

8 pages

key

key

6 pages

21-OOP

21-OOP

8 pages

Load more
Download 11-modules-info-out
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view 11-modules-info-out and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view 11-modules-info-out 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?