150 likes | 284 Vues
How to design and code functions. Chapter 4 (ctd). 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.
E N D
How to design and code functions Chapter 4 (ctd)
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
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, hourlywage, weekly pay printResults
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)
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)
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
Code each function Function getHours inputs and returns an integer value for hours worked (between 0 and 60): intgetHours() { int hours; do { printf("Enter hours worked: "); scanf("%d", &hours); if (hours < 0 || hours > 60) printf("Invalid hours (0-60)\n"); } while (hours < 0 || hours > 60); return hours; }
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 < 10.25 || rate > 20.00) printf("Invalid rate (10.25-20.00)\n"); } while (rate < 10.25 || rate > 20); return rate; }
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 ; }
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) ; }
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) ;}
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 < 0 || hrs > 60) printf(“Invalid hrs (0-60)\n”); } while (hours < 0 || hours > 60); return hrs; }
Assemble and test program (ctd) Next add getRate to program and test: int getHours(); float getRate(); void main (){ … } int getHours() { … } float getRate() { … }
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) { … }
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) { … }