{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture 10 - Lecture 10 Oct 7 02 Pass by value and...

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern