- 89 Views
- Uploaded on
- Presentation posted in: General

How to design and code functions

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

How to design and code functions

Chapter 4 (ctd)

- 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 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

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)

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)

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

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;

}

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;

}

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 ;

}

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) ;

}

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 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;

}

Next add getRate to program and test:

int getHours();

float getRate();

void main (){ … }

int getHours()

{… }

float getRate()

{… }

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)

{… }

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)

{… }