Call-By-Reference Example: Display 4.1 Call-by-Reference Parameters(2 of 3) 18 void getNumbers(int&input1,int&input2) 19 20 cout <"Enter two integers:" 21 cin >inputl 2 >input2; 3 3 24 void swapValues(int&variablel,int&variable2) 25 26 int temp; 27 temp variablel; 28 variablel variable2; variable2 temp; 0 32 void showResults(int outputl,int output2) 3 { 34 cout <<"In reverse order the numbers are: <outputl <""<output2 <endl; 36 Copyright006 Pearson Addison-Wesley.All rights reserved. 4-11
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-11 Call-By-Reference Example: Display 4.1 Call-by-Reference Parameters (2 of 3)
Call-By-Reference Example: Display 4.1 Call-by-Reference Parameters(3 of 3) Display 4.2 Call-by-Reference Parameters SAMPLE DIALOGUE Enter two integers:5 6 In reverse order the numbers are:65 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 4-12
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-12 Call-By-Reference Example: Display 4.1 Call-by-Reference Parameters (3 of 3)
Call-By-Reference Details +What's really passed in? .A "reference"back to caller's actual argument! Refers to memory location of actual argument Called "address",which is a unique number referring to distinct place in memory Copyright 2006 Pearson Addison-Wesley.All rights reserved. 413
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-13 Call-By-Reference Details ¨ What’s really passed in? ¨ A "reference" back to caller’s actual argument! ¨Refers to memory location of actual argument ¨Called "address" , which is a unique number referring to distinct place in memory
Constant Reference Parameters Reference arguments inherently "dangerous" Caller's data can be changed Often this is desired,sometimes not To "protect"data,still pass by reference: ◆Use const keyword void sendConstRef(const int &par1, const int &par2); Makes arguments "read-only"by function No changes allowed inside function body Copyright 2006 Pearson Addison-Wesley.All rights reserved. 4-14
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-14 Constant Reference Parameters ¨ Reference arguments inherently "dangerous" ¨ Caller’s data can be changed ¨ Often this is desired, sometimes not ¨ To "protect" data, & still pass by reference: ¨ Use const keyword ¨ void sendConstRef( const int &par1, const int &par2); ¨ Makes arguments "read-only" by function ¨ No changes allowed inside function body