1. Modules A module is a group of statements that exist within a program for the purpose of performing a specific task Module main() Display “How are you doing on IT104?” Call finalGrade() //define calling a module Display “Congratulations!” End Module Module finalGrade() Display “you’ve worked very hard” Display “Your final grade is A.” End Module
2. Functions With Module pp.230 Constant Real discount_percentage = 0.20 //Global constants Module main() Declare real regularPrice, salePrice//local variables Set regularPrice = getRegularPrice() set salePrice = regularPrice – discount (regularPrice) Display “The sale price is $”, salesPrice End Module Function Real getRegularPrice() Declare Real price Display “Enter the regular price.” Input price Return price End Function Function Real discount (real price) Return price * discount_percentage End Function A function is a module that returns a value back to the part of the program that called it
3. Compare /Contrast (lab 2.2) //Declare variables Declare Real stateTax Declare Real countyTax Declare Real totalTax Declare Real totalSales //Get data Display “Enter the total sales.” Input totalSales //Calculate taxes Set stateTax = totalSales * 0.04 Set countyTax = totalSales * 0.02 Set totalTax = stateTax + countyTax //Output data Display “The total tax is”, totalTax Model main() //Declare local variables Declare Real stateTax Declare Real countyTax Declare Real totalTax Declare Real totalSales //Function calls Call inputData (totalSales) Call calcState (totalSales) Call calcCounty (totalSales) Call calcTotal (countyTax, stateTax, totalTax) Call printData (countyTax, stateTax, totalTax) End Module
4. Passing Argument to Modules(Lab 2.2) Module main() //Declare local variables Declare Real stateTax Declare Real countyTax Declare Real totalTax Declare Real totalSales //Function calls Call inputData (totalSales) //passing arguments Call calcState (totalSales) Call calcCounty (totalSales) Call calcTotal (countyTax, stateTax, totalTax) Call printData (countyTax, stateTax, totalTax) End Module Module inputData (Real Ref totalSales) Display “Enter the total sales.” Input totalSales End Module Module calcState (Real totalSales, Real Ref stateTax) stateTax = totalSales * 0.04 End Module Module calcCounty (Real totalSales, Real Ref CountTax) countyTax = totalSales * 0.02 End Module Module calcTotal (Real countyTax, Real stateTax, Real Ref totalTax) totalTax = stateTax + countyTax End Module Module printData (Real countyTax, Real stateTax, Real totalTax) Display “The total tax is”, totalTax End module