1 / 15

How to design and code functions

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.

conway
Download Presentation

How to design and code functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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, hourlywage, 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): 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; }

  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 < 10.25 || rate > 20.00) printf("Invalid rate (10.25-20.00)\n"); } while (rate < 10.25 || 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 < 0 || hrs > 60) printf(“Invalid hrs (0-60)\n”); } while (hours < 0 || 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) { … }

More Related