Modules: Getting information outBy default, Pascal passes parameters by value. So how do we get information out of a module?Retaining Information From A ModuleFunctionsDefining FunctionsCalling FunctionsTracing The Execution Of A Function CallSlide 8Functions: Putting It All TogetherFunctions: Putting It All Together (2)Functions: Putting It All Together (3)Functions: Putting It All Together (4)Functions: Putting It All Together (5)Slide 14Passing Parameters By ________Passing Parameters by _______Passing Parameters by ___________ (As ___________ Parameters)Slide 18Procedure Definitions When Passing Parameters As ____________ ParametersCalling Procedures With __________ ParametersLimitations on Actual ParametersVariable Parameters: A Simple ExamplePassing Variable Parameters: Putting Together A Solution To A Real ProblemPassing Variable Parameters: Putting It All Together (2)Passing Variable Parameters: Putting It All Together (3)Passing Variable Parameters: Putting It All Together (4)Passing Variable Parameters: Putting It All Together (5)Passing Variable Parameters: Putting It All Together (6)Functions Vs. Variable ParametersWhere To Declare Variables?Where To Declare Variables: ExampleSlide 32Where To Declare Variables: Solution To The ExampleVariable Parameters And Multiple Module CallsVariable Parameters And Multiple Module Calls (3)Variable Parameters And Multiple Module Calls (2)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);var grossProfit : real;begin grossProfit := 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; begin calculateGrossIncome := 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;var grossSales : real; costOfGoodsSold : real;begin grossIncome := calculateGrossIncome (grossSales, costOfGoodsSold);function calculateGrossIncome (grossSales : real; costOfGoodsSold : real) : real;begin calculateGrossIncome := 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;begin calculateGrossIncome := grossSales - costOfGoodsSoldend; function calculateNetIncome (grossIncome : real; expenses : real) : real;begin calculateNetIncome := grossIncome - expenses;end;J. Michael MooreFunctions: 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 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;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 MooreFunctions: Putting It All Together (5)(* Start of main program *)begin intro; 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
View Full Document