Lecture 10 Oct 7, 02
Pass by value and reference ► Calling a function and passing the value of a variable as an argument is called pass by value. The examples we have till now explained are for pass by value. ► Calling a function and passing the address of a variable as an argument that will be accepted as a reference parameter is called pass by reference
Pass by value ( Lec 9 eg ) ► #include ► #include int calsum(int, int, int) // function prototype declaration int calsum(int, int, int) // function prototype declaration int main() int main() { int a,b,c,sum; int a,b,c,sum; cout<<“Enter any three numbers”<<endll cout<<“Enter any three numbers”<<endll cin>>a>>b>>c; cin>>a>>b>>c; sum=calsum(a,b,c); // function call done here sum=calsum(a,b,c); // function call done here return 0; return 0; } // The function calsum // The function calsum int calsum(int x, int y, int z) // function header int calsum(int x, int y, int z) // function header { int d; int d; d = x+y+z; d = x+y+z; return(d); return(d); }
Pass by reference (example 1) ► #include ► #include void newval(float&, float&); // function prototype decaration void newval(float&, float&); // function prototype decaration int main() int main() { float firstnum, secnum; float firstnum, secnum; cout<<“Enter two numbers :”; cout<<“Enter two numbers :”; cin>>firstnum>>secnum; cin>>firstnum>>secnum; cout<<“\nThe value in first num is: “<<firstnum<<endl; cout<<“\nThe value in first num is: “<<firstnum<<endl; cout<<“The value in secnum is: “<<secnum<<endl; cout<<“The value in secnum is: “<<secnum<<endl; newval(firstnum, secnum); // call the function newval(firstnum, secnum); // call the function cout<< “ The value in firstnum is now: “<<firstnum<<endl; cout<< “ The value in firstnum is now: “<<firstnum<<endl; cout<< “ The value in secnum is now: “<<secnum<<endl; cout<< “ The value in secnum is now: “<<secnum<<endl; return 0; return 0; }
newval function ► void newval(float& xnum, float& ynum) // function header { cout<< “ The value in xnum is: “<<xnum<<endl; cout<< “ The value in xnum is: “<<xnum<<endl; cout<< “ The value in ynum is: “<<ynum<<endl; cout<< “ The value in ynum is: “<<ynum<<endl; xnum = 89.5; xnum = 89.5; ynum = 99.5; ynum = 99.5; return; return; } / * float& xnum read as “ xnum is the address of a floating point value */ / * float& xnum read as “ xnum is the address of a floating point value */
Example 2 ► #include ► #include void calc(float, float, float&, float&); // function prototype void calc(float, float, float&, float&); // function prototype int main() int main() { float firstnum, secnum, thirdnum, sum, product; float firstnum, secnum, thirdnum, sum, product; cout<<“ Enter three numbers: ”; cout<<“ Enter three numbers: ”; cin >>firstnum>>secnum>>thirdnum; cin >>firstnum>>secnum>>thirdnum; calc(firstnum, secnum, thirdnum, sum, product); // function call calc(firstnum, secnum, thirdnum, sum, product); // function call cout<<“\nThe sum of the numbers is: “<<sum<<endl; cout<<“\nThe sum of the numbers is: “<<sum<<endl; cout<<“The product of the numbers is: “<<product<<endl; cout<<“The product of the numbers is: “<<product<<endl; return 0; return 0; }
calc function ► void calc(float num1, float num2, float num3, float& total, float& product) { total = num1 + num2 + num3; total = num1 + num2 + num3; product = num1 * num2 * num3; product = num1 * num2 * num3; return; return; }
Example 3 ( swap 2 values) ► #include ► #include void swap(float&, float&); // function receives 2 references void swap(float&, float&); // function receives 2 references int main() int main() { float firstnum = 20.5, secnum = 6.25; float firstnum = 20.5, secnum = 6.25; cout<<“The value stored in firstnum is: ”<<firstnum<<endl; cout<<“The value stored in firstnum is: ”<<firstnum<<endl; cout<<“The value stored in secnum is : ”<<secnum<<endl; cout<<“The value stored in secnum is : ”<<secnum<<endl; swap(firstnum, secnum); // function call swap(firstnum, secnum); // function call cout<<“The value stored in firstnum is now: ”<<firstnum<<endl; cout<<“The value stored in firstnum is now: ”<<firstnum<<endl; cout<<“The value stored in secnum is now: ”<<secnum<<endl; cout<<“The value stored in secnum is now: ”<<secnum<<endl; return 0; return 0; }
swap function ► void swap(float& num1, float& num2) { float temp; float temp; temp = num1; // save num1’s value temp = num1; // save num1’s value num1 = num2; // store num2’s value num1 = num2; // store num2’s value num2 = temp; // change num2’s value num2 = temp; // change num2’s value return; return; }
Difference between pass by value and pass by reference ► value : value of variable passed reference : address of variable passed reference : address of variable passed ► value : int calsum(int, int, int) // function prototype declaration reference : void swap( float&, float&); reference : void swap( float&, float&); ► value : int calsum(int x, int y, int z) // function header reference : void swap(float& num1, float& num2) reference : void swap(float& num1, float& num2) ► value : copy of value is stored in the called function reference : value changes as the address is referenced reference : value changes as the address is referenced