Call-by-Value Example: Display 4.1 Formal Parameter Used as a Local Variable (2 of 3) 12 cout <"Welcome to the law office of\n" 13 <<"Dewey,Cheatham,and Howe.\n" 14 <"The law office with a heart.\n" The value of minutes 15 <"Enter the hours and minutes" is not changed by the 16 <<"of your consultation:\n"; call to fee. 17 cin >hours >minutes; 18 bill fee(hours,minutes); 19 cout.setf(ios:fixed); 20 cout.setf(ios:showpoint); 21 cout.precision(2); 2 cout <"For "<hours <<"hours and "<minutes <<"minutes,your bill is $"<bill <endl; 24 return 0; 25 } (continued) Copyright2006 Pearson Addison-Wesley.All rights reserved. 4-6
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-6 Call-by-Value Example: Display 4.1 Formal Parameter Used as a Local Variable (2 of 3)
Call-by-Value Example: Display 4.1 Formal Parameter Used as a Local Variable (3 of 3) Display 4.1 Formal Parameter Used as a Local Variable 26 double fee(int hoursWorked,int minutesWorked) minutesWorked is a local 27 variable initialized to the 28 int quarterHours; value of minutes. 29 minutesWorked hoursWorked*60 minutesWorked; 30 quarterHours minutesWorked/15; 31 return (quarterHours*RATE); 32 SAMPLE DIALOGUE Welcome to the law office of Dewey,Cheatham,and Howe. The law office with a heart. Enter the hours and minutes of your consultation: 546 For 5 hours and 46 minutes,your bill is $3450.00 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 47
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-7 Call-by-Value Example: Display 4.1 Formal Parameter Used as a Local Variable (3 of 3)
Call-by-Value Pitfall ◆Common Mistake: Declaring parameter "again"inside function: double fee(int hoursWorked,int minutesWorked) int quarterHours; 1/local variable int minutesWorked /NO! } Compiler error results "Redefinition error." Value arguments ARE like "local variables" But function gets them "automatically" Copyright 2006 Pearson Addison-Wesley.All rights reserved. 4-8
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-8 Call-by-Value Pitfall ¨ Common Mistake: ¨ Declaring parameter "again" inside function: double fee(int hoursWorked, int minutesWorked) { int quarterHours; // local variable int minutesWorked // NO! } ¨ Compiler error results ¨ "Redefinition error." ¨ Value arguments ARE like "local variables" ¨ But function gets them "automatically
Call-By-Reference Parameters Used to provide access to caller's actual argument Caller's data can be modified by called function! Typically used for input function To retrieve data for caller Data is then "given"to caller Specified by ampersand,&after type in formal parameter list Copyright 2006 Pearson Addison-Wesley.All rights reserved. 4-9
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-9 Call-By-Reference Parameters ¨ Used to provide access to caller’s actual argument ¨ Caller’s data can be modified by called function! ¨ Typically used for input function ¨ To retrieve data for caller ¨ Data is then "given" to caller ¨ Specified by ampersand, &, after type in formal parameter list
Call-By-Reference Example: Display 4.1 Call-by-Reference Parameters (1 of 3) Display 4.2 Call-by-Reference Parameters 1 //Program to demonstrate call-by-reference parameters. 2 #include <iostream> 3 using namespace std; 4 void getNumbers(int&input1,int&input2); 5 //Reads two integers from the keyboard. 6 void swapValues(int&variablel,int&variable2); 7 //Interchanges the values of variablel and variable2. 8 void showResults(int outputl,int output2); 9 //Shows the values of variablel and variable2,in that order. 10 int main() 11 { 12 int firstNum,secondNum; 13 getNumbers(firstNum,secondNum); swapValues(firstNum,secondNum); 15 showResults(firstNum,secondNum); return 0; 17 Copyright006 Pearson Addison-Wesley.All rights reserved. 4-10
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-10 Call-By-Reference Example: Display 4.1 Call-by-Reference Parameters (1 of 3)