Communication between Functions
Assignment 6 - Void Functions vs Value-returning Functions
Another way that the called function can communicate a result back to the calling function is through the function value. In this exercise, you will start with a void function and convert it to a value-returning function.
Here is a payroll program.
//***************************************************** // Program 6: Payroll Program // Programmer: Your Name // Completed : date // Status : Complete // // This program calculates and prints your weekly pay. //***************************************************** #include <iostream.h> #include <iomanip.h> void CalcPay(float hoursWorked, float payRate, float& pay); int main() { float hoursWorked; // hours worked in a week float payRate; // hourly pay rate in dollars float pay; // weekly pay in dollars // Set up floating point output format cout << setiosflags(ios::fixed) << setprecision(2); // Set the number of hours worked and hourly pay rate hoursWorked = 35.0; payRate = 10.5; // Calculate the weekly pay CalcPay(hoursWorked, payRate, pay); // Print hours worked, pay rate, and your weekly pay cout << "Your weekly pay information" << endl; cout << " Hours worked: " << setw(8) << hoursWorked << endl; cout << " Pay rate : " << setw(8) << payRate << endl; cout << " Weekly pay : " << setw(8) << pay << endl << endl; return 0; } //********************************************************** // Function CalcPay() // // Purpose - this function calculates your weekly pay and // returns it to the calling function in parameter pay. //********************************************************** void CalcPay( float hoursWorked, // hours worked in a week float payRate, // hourly pay rate in dollars float& pay) // weekly pay in dollars { // Calculate the weekly pay pay = hoursWorked * payRate; }
Exercise Steps:
pay = CalcPay(hoursWorked, payRate);
Your new program should still produce the same output as before.
Exercise Deliverables: