SLIDE08 - Exposure C Chapter VIII Program Modularity and...

This preview shows page 1 - 11 out of 46 pages.

Exposure C++ Chapter VIII Program Modularity and Functions
Image of page 1

Subscribe to view the full document.

Divide and Conquer with Modules Divide a program into modules that focus on a singular task that is manageable. Regardless of size, continue to divide any program segment that is too complex until a manageable task is reached. A popular computer science saying says: One task --- one module Ideally one program module should print on one piece of paper. If a module becomes too large, divide it into smaller modules.
Image of page 2
// PROG0801.CPP #include <iostream.h> #include <iomanip.h> void main() { // VARIABLE DEFINITIONS double PayRate; // hourly wage int HoursWorked; // hours worked per week double GrossPay; // hours worked times hourly wage double Deductions; // taxes and social security payments double NetPay; // take-home pay after deductions // ENTER DATA SEGMENT cout << "ENTER YOUR HOURLY PAYRATE ===>> "; cin >> PayRate; cout << "ENTER YOUR WEEKLY HOURS ===>> "; cin >> HoursWorked; // continue with next slide
Image of page 3

Subscribe to view the full document.

//PROCESS DATA SEGMENT GrossPay = PayRate * HoursWorked; Deductions = GrossPay * 0.28; NetPay = GrossPay - Deductions; // DISPLAY DATA SEGMENT cout << endl << endl; cout << setiosflags(ios::fixed); cout << setiosflags(ios::showpoint); cout << setprecision(2); cout << "GROSSPAY: " << GrossPay << endl; cout << "DEDUCTIONS: " << Deductions << endl; cout << "NETPAY: " << NetPay << endl; }
Image of page 4
User-Created Function Syntax void Module1() { cout << "This is Module1" << endl; } void Module2() { cout << endl << endl; cout << "This is Module2" << endl; } void Module3() { cout << endl << endl; cout << "This is Module3" << endl; } PROG0802.CPP OUTPUT This is Module1 This is Module2 This is Module3 void main() { Module1(); Module2(); Module3(); }
Image of page 5

Subscribe to view the full document.

Function Components void Example() // function heading { // start of function body cout << endl; cout << "Hello There"; } // end of function body
Image of page 6
// PROG0803.CPP // This program divides the earlier program into void function modules. // This program does not compile because the variables are defined "locally" // inside a function. #include <iostream.h> #include <iomanip.h> void Variables() { double PayRate; // hourly wage int HoursWorked; // hours worked per week double GrossPay; // hours worked times hourly wage double Deductions; // taxes and social security payments double NetPay; // take-home pay after deductions } // continue with next slide
Image of page 7

Subscribe to view the full document.

// continue with PROG0803.CPP void EnterData() { cout << "ENTER YOUR HOURLY PAYRATE ===>> "; cin >> PayRate; cout << "ENTER YOUR WEEKLY HOURS ===>> "; cin >> HoursWorked; } void ProcessData() { GrossPay = PayRate * HoursWorked; Deductions = GrossPay * 0.28; NetPay = GrossPay - Deductions; } // continue with next slide
Image of page 8
// continue with PROG0803.CPP void DisplayData() { cout << endl << endl; cout << setiosflags(ios::fixed); cout << setiosflags(ios::showpoint); cout << setprecision(2); cout << "GROSSPAY: " << GrossPay << endl; cout << "DEDUCTIONS: " << Deductions << endl; cout << "NETPAY: " << NetPay << endl; } void main() { Variables(); EnterData(); ProcessData(); DisplayData(); } This program does not compile. All the variables are defined "locally"
Image of page 9

Subscribe to view the full document.

Local and Global Variables A local variable is defined inside a function and can only be accessed inside that function.
Image of page 10
Image of page 11
  • Fall '16
  • Ms. Walker
  • Control flow

{[ snackBarMessage ]}

Get FREE access by uploading your study materials

Upload your study materials now and get free access to over 25 million documents.

Upload now for FREE access Or pay now for instant access
Christopher Reinemann
"Before using Course Hero my grade was at 78%. By the end of the semester my grade was at 90%. I could not have done it without all the class material I found."
— Christopher R., University of Rhode Island '15, Course Hero Intern

Ask a question for free

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern