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