Presentation is loading. Please wait.

Presentation is loading. Please wait.

How to design and code functions Chapter 4 (ctd).

Similar presentations


Presentation on theme: "How to design and code functions Chapter 4 (ctd)."— Presentation transcript:

1 How to design and code functions Chapter 4 (ctd)

2 Functions Example Write a program using functions to calculate weekly pay for an employee Inputs include: number of hours worked (assume only whole numbers) and hourly wage for the employee. Weekly pay to be calculated using overtime policy Output: hours worked, hourly wage and weekly pay

3 Identify tasks Identify separate tasks to be performed by program. Program should be coded using a function with a meaningful name for each task as follows: ask user to enter number of hours worked  getHours ask user to enter hourly wage  getRate calculate weekly pay using overtime policy  calculatePay print hours worked, hourly wage, weekly pay  printResults

4 Identify values to be passed to function (input parameters) Next step is to identify values required (i.e. input parameters) by each function as follows: getHours: no values to be passed  getHours() getRate: no values to be passed  getRate() calculatePay: to be passed hours and rate  calculatePay(int hours, float rate) printResults: to be passed hours, rate and pay  printResults(int hours, float rate, float pay)

5 Identify value to be returned by function (return value) Next step is to identify value to be returned by each function as follows: getHours: return hours worked  int getHours() getRate: return hourly rate  float getRate() calculatePay: return weekly pay  float calculatePay(int hours, float rate) printResults: no values to be returned  void printResults(int hours, float rate, float pay)

6 Identify value to be returned by function (return value) Next step is to identify value to be returned by each function as follows: getHours: return hours worked  int getHours() getRate: return hourly rate  float getRate() calculatePay: return weekly pay  float calculatePay(int hours, float rate) printResults: no values to be returned  void printResults(int hours, float rate, float pay) Use these as your function prototypes

7 Code each function Function getHours inputs and returns an integer value for hours worked (between 0 and 60): int getHours() { int hours; do { printf("Enter hours worked: "); scanf("%d", &hours); if (hours 60) printf("Invalid hours (0-60)\n"); } while (hours 60); return hours; }

8 Code each function (ctd) Function getRate inputs and returns a float value for hourly rate (between 10.25 and 20): float getRate(){ float rate; do { printf("Enter hourly rate: "); scanf("%f", &rate); if (rate 20.00) printf("Invalid rate (10.25-20.00)\n"); } while (rate 20); return rate; }

9 Code each function (ctd) Function calculatePay calculates weekly pay paying hourly rate for first 40 hours and time and a half for overtime hours: float calculatePay(int hours, float rate) {float pay; if (hours > 40) pay = 40*hours * rate + (hours-40)*1.5 * rate ; else pay = hours * rate ; return pay ; }

10 Code each function(ctd) Function printResults displays hourly rate, hours worked and pay: void printResults(int hours, float rate, float pay) { printf ("*** Weekly Pay ***\n") ; printf ("*****************\n") ; printf ("Hours worked: %d\n", hours) ; printf ("Hourly rate: $%.2lf\n", rate) ; printf ("Pay: $%.2lf\n", pay) ; }

11 Code main function Function main calls getHours, getRate, calculatePay and printResults: void main () { int hours ; float rate, pay ; hours = getHours ( ) ; rate = getRate ( ) ; pay = calculatePay (hours, rate) ; printResults (hours, rate, pay) ; }

12 Assemble and test program Assemble program by adding functions to main (start with input functions) and testing thoroughly: int getHours(); void main () { int hours ; float rate, pay ; hours = getHours ( ) ; printf(“Hours: %d\n”,hours); /* for testing only */ } int getHours() {int hrs; do { printf(“Enter hours worked: “); scanf(“%d”, &hrs); if (hrs 60) printf(“Invalid hrs (0-60)\n”); } while (hours 60); return hrs; }

13 Assemble and test program (ctd) Next add getRate to program and test: int getHours(); float getRate(); void main () { … } int getHours() {… } float getRate() {… }

14 Assemble and test program (ctd) Next add calculatePay to program and test: int getHours(); float getRate(); float calculatePay(int hours, float rate); void main () { … } int getHours() {… } float getRate() {… } float calculatePay(int hours, float rate) {… }

15 Assemble and test program (ctd) Now add printResults to program and test: int getHours(); float getRate(); float calculatePay(int hours, float rate); void printResults(int hours, float rate, float pay); void main () { … } int getHours() {… } float getRate() {… } float calculatePay(int hours, float rate) {… } void printResults(int hours, float rate, float pay) {… }


Download ppt "How to design and code functions Chapter 4 (ctd)."

Similar presentations


Ads by Google