
Chapter 4 ABSOLUTE C++ Parameters and Overloading WALTER SAVITCH SECOND EDITION PEARSON Copyright2006 Pearson Addison-Wesley All rights reserved
Chapter 4 Parameters and Overloading

Learning Objectives ◆Parameters ◆Call-by-value ◆Call-by-reference Mixed parameter-lists Overloading and Default Arguments ◆Examples,Rules Testing and Debugging Functions ◆assert Macro ◆Stubs,Drivers Copyright006 Pearson Addison-Wesley.All rights reserved. 4-2
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Learning Objectives ¨ Parameters ¨ Call-by-value ¨ Call-by-reference ¨ Mixed parameter-lists ¨ Overloading and Default Arguments ¨ Examples, Rules ¨ Testing and Debugging Functions ¨ assert Macro ¨ Stubs, Drivers

Parameters Two methods of passing arguments as parameters ◆Call-by-value "copy"of value is passed ◆Call-by-reference "address of"actual argument is passed Copyright 2006 Pearson Addison-Wesley.All rights reserved. 4-3
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-3 Parameters ¨ Two methods of passing arguments as parameters ¨ Call-by-value ¨"copy" of value is passed ¨ Call-by-reference ¨"address of" actual argument is passed

Call-by-Value Parameters Copy of actual argument passed Considered "local variable"inside function If modified,only "local copy"changes Function has no access to "actual argument" from caller This is the default method Used in all examples thus far Copyright006 Pearson Addison-Wesley.All rights reserved. 4-4
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-4 Call-by-Value Parameters ¨ Copy of actual argument passed ¨ Considered "local variable" inside function ¨ If modified, only "local copy" changes ¨ Function has no access to "actual argument" from caller ¨ This is the default method ¨ Used in all examples thus far

Call-by-Value Example: Display 4.1 Formal Parameter Used as a Local Variable (1 of 3) Display 4. Formal Parameter Used as a Local Variable 1 //Law office billing program. 3 #include 3 using namespace std; 4 const double RATE =150.00;//Dollars per quarter hour. 5 double fee(int hoursWorked,int minutesWorked); 6 //Returns the charges for hoursWorked hours and 7 //minutesWorked minutes of legal services. 8 int main() 9£ 10 int hours,minutes; 11 double bill; Copyright 2006 Pearson Addison-Wesley.All rights reserved. 4-5
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-5 Call-by-Value Example: Display 4.1 Formal Parameter Used as a Local Variable (1 of 3)

Call-by-Value Example: Display 4.1 Formal Parameter Used as a Local Variable (2 of 3) 12 cout 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 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)