Top Banner
1 C++ Functions
23

1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.

Dec 17, 2015

Download

Documents

Arabella Kelly
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.

1

C++ Functions

Page 2: 1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.

// The function computes and returns the gross pay

// based on the pay rate and hours. Hours over

// 40 will be paid 1.5 times the regular pay rate.

float GrossPay(float payRate, float hours)

{

float total;

if (hours > REG_HOURS)

total = (hours - REG_HOURS)* OVER_TIME * payRate +

REG_HOURS * payRate;

else

total = hours * payRate;

return total;

}

// No input for payRate and hours

// Parameters receive values on function call!

2

Page 3: 1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.

Passing Parameters

const float REG_HOURS = 40.0;const float OVER_TIME = 1.5;

int main(){ float hours, rate, gross;

cin >> rate >> hours; // rate: 12.5 // hours: 50 gross = GrossPay(rate, hours);

// display result

return 0;}

int GrossPay(float payRate, float hours){ float total; if (hours > REG_HOURS) total = (hours - REG_HOURS) * OVER_TIME * payRate + REG_HOURS * payRate; else total = hours * payRate; return total;}

main() GrossPay()

Function call

Passing parameters

12.5 50

Returning to main()

With return value

3

Page 4: 1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.

IN Parameter

The value of the actual parameter is passed into the function and assigned to the formal parameters.

float GrossPay(float payRate, float hours);

int main(){ ... cin >> rate >> hours; // rate: 12.5 // hours: 45.5 gross = GrossPay(rate, hours); ...}

4

Page 5: 1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.

IN Parameters

int Largest(int num1, int num2, int num3);

cin >> score1 >> score2 >> score3;max = Largest(score1, score2, score3);

void DisplayResult(float avg, float max,

float min);

// Input scores and compute the highest, // lowest and average

DisplayResult(avg, highest, lowest);// The function does output, but IN parameters!

float sqrt(float x);

5

Page 6: 1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.

Function Parameters

In The value of the actual parameter is

passed into the function and assigned to the formal parameter.

Out The value of formal parameter is

passed out of the function and assigned to the actual parameter.

InOut Both In and Out.

6

Page 7: 1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.

float GrossPay(float payRate, float hours);

int main(){ float hours, rate, gross;

// Input values in main() cin >> rate >> hours;

gross = GrossPay(rate, hours);

// display result

return 0;}

// Q: Can we use a function to input rate and hours?

7

Page 8: 1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.

Write a function to input rate and hours

Function Prototype

Name: GetInput

Type: void Cannot pass two values using the return statement

Parameters: rate (payRate), hours (hoursWorked) type: float (Passing values back to calling function) (OUT parameters!) (&)

void GetInput(float& payRate, float& hoursWorked);

8

Page 9: 1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.

float GrossPay(float payRate, float hours);void GetInput(float& payRate, float& hoursWorked);

int main(){ float hours, rate, gross;

// Call function GetInput() to get two values GetInput(rate, hours);

// Call function GrossPay to get one value gross = GrossPay(rate, hours);

// display result

return 0;}

9

Page 10: 1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.

Function Definition// -------------------------------------------------// The function inputs payRate and hoursWorked and// pass both values back to the calling function.// Parameters: (out, out)// -------------------------------------------------void GetInput(float& payRate, float& hoursWorked){ cout << "Enter pay rate: "; cin >> payRate; cout << "Enter hours: "; cin >> hoursWorked;

return;}

// The function does input with prompt, but OUT parameters!// How can it pass two values back?// Out Parameters: &// Statement return can pass only one value back!

10

Page 11: 1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.

// -------------------------------------------------

// The function computes and returns the gross pay

// based on the pay rate and hours. Hours over

// 40 will be paid 1.5 times the regular pay rate.

// Parameters: (in, in)

// -------------------------------------------------

float GrossPay(float payRate, float hours)

{

if (hours > REG_HOURS)

payRate = (hours - REG_HOURS) * OVER_TIME * payRate +

REG_HOURS * payRate;

else

payRate = hours * payRate;

return payRate;

}

// No local variable

// payRate is used to store the result

11

Page 12: 1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.

Reference and Value Parametersfloat GrossPay(float payRate, float hours);

void GetInput(float& payRate, float& hoursWorked);

Value parameter: No &

The value of actual parameter is passed to the formal parameter

Reference Parameter: &

The address of actual parameter is passed to the formal parameter

Does the actual parameter change its value when the corresponding formal parameter changes its value?

Value parameter (no &): NO

Reference parameter (&): YES

12

Page 13: 1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.

Value and Reference Parameters

const float REG_HOURS = 40.0;const float OVER_TIME = 1.5;

int main(){ float hours, rate, gross;

GetInput(rate, hours);

gross = GrossPay(rate, hours);

// display result

return 0;}

float GrossPay(float payRate, float hours){ if (hours > REG_HOURS) payRate = (hours - REG_HOURS) * OVER_TIME * payRate + REG_HOURS * payRate; else payRate = hours * payRate; return payRate;}

main()

GetInput()

void GetInput(float& payRate, float& hoursWorked){ cout << "Enter pay rate: "; cin >> payRate; cout << "Enter hours: "; cin >> hoursWorked;

return;}

GrossPay()Passing parameters

12.5 50

Passing parameters

Addresses of rate and hours

Return control

With value

Return control

13

Page 14: 1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.

Tracing FunctionsInput: 10 45

HiCNotes\HiC\GrossPayInOutFuns.cpp

14

Page 15: 1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.

Tracing FunctionsInput: 10 45

GetInput(rate, hours); // Reference parametersgross = GrossPay(rate, hours);// Value parameters

main() GetInput() GrossPay() hours rate gross & payRate & hoursWorked hours payRate ? ? ? ? ? ? ? Addr of Addr of rate hours 10.0 45.0 45.0 10.0 475.0 475.0

15

Page 16: 1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.

Schedule

• Program 2

Grace Time: 9:30 PM, Today

Style!

Name conversion

• Quiz 4-2

Due 5 pm Monday

• Program 3

Two points Quiz Wednesday

Two points progress Lab Thursday

Discuss Friday

16

Page 17: 1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.

17

Prog2 Winner

// The new route is the winner

if (totalDistance < minTotalDistance &&

numDeiveries > maxDeiveries)

// update winnerNum,

// minTotalDistance and maxDeiveries

// No winner for now

else if (totalDistance =< minTotalDistance ||

numDeiveries >= maxDeiveries)

// update winnerNum to -1

// minTotalDistance and maxDeiveries

// The previous winner still winner

else

// do nothing

Page 18: 1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.

18

Prog2 Winner

if (totalDistance < minTotalDistance &&

numDeiveries > maxDeiveries)

. . .

// No winner for now

else if (totalDistance =< minTotalDistance ||

numDeiveries >= maxDeiveries)

{

winnerNum = -1;

minTotalDistance = -1;

maxDeiveries = -1;

}

Incorrect!

Not Style!

Page 19: 1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.

19

Prog2 Winner

// No winner for now

else if (totalDistance =< minTotalDistance ||

numDeiveries >= maxDeiveries)

{

winnerNum = -1;

if (totalDistance < minTotalDistance)

minTotalDistance = totalDistance;

if (numDeiveries > maxDeiveries)

maxDeiveries = numDeiveries;

}

Correct!

Update your Prog2 and submit again!

Page 20: 1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.

20

Prog2 Winner: Another Wayif (totalDistance < minTotalDistance &&

numDeiveries > maxDeiveries)

. . .

else

{

if (totalDistance =< minTotalDistance)

{

winnerNum = -1;

minTotalDistance = totalDistance;

}

// No else !

if (numDeiveries >= maxDeiveries)

{

winnerNum = -1;

maxDeiveries = numDeiveries;

}

}

Page 21: 1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.

21

Prog2 Winner: Another Wayif (routeNum == 1)

{

distWinnerNum = routeNum;

delWinnerNum = routeNum;

minTotalDistance = totalDistance;

maxDeiveries = numDeiveries;

}

else

{

if (totalDistance < minTotalDistance)

{

distWinnerNum = routeNum;

minTotalDistance = totalDistance;

}

else if (totalDistance == minTotalDistance)

{

distWinnerNum = routeNum;

}

// else prev distWinnerNum still winner

// same if – else if for delWinnerNum

}

Page 22: 1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.

Exercise

Tracing Functions with In and Out Parameters

GrossPayInOutParam.doc

22

Page 23: 1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.

Tracing FunctionsInput: 10 45

GetInput(rate, hours); // Reference parametersgross = GrossPay(rate, hours);// Value parameters

main() GetInput() GrossPay() hours rate gross & payRate & hoursWorked hours payRate ? ? ? ? ? ? ? Addr of Addr of rate hours 10.0 45.0 45.0 10.0 475.0 475.0

23