passref - Programming Functions Passing Parameters by...

Info icon This preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Programming Functions: Passing Parameters by Reference
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2: Prog. Fundamentals: Pass by Reference/Slide 2 Passing Parameters by Reference To have a function with multiple outputs, we have to use pass by reference. We use & to denote a parameter that is passed by reference: <type> & <variable> Examples : void Increment( int & Number); void SumAve ( double , double , double &, double &);
Image of page 2
2: Prog. Fundamentals: Pass by Reference/Slide 3 Passing Parameters by Reference The corresponding argument must be a variable. Increment(Inc); SumAve (2.5, y+3, sum, mean); The address (reference) of that variable is passed to the function, instead of its value. If the function changes the parameter value, the change will be reflected in the corresponding argument , since they share the same memory location.
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2: Prog. Fundamentals: Pass by Reference/Slide 4 Pass by Reference: Example 1 To show how the function affects a variable which is used as an argument: #include <iostream> using namespace std; void Increment( int & Number){ Number = Number + 1; cout << "The parameter Number: " << Number << endl; } int main(){ int Inc = 10; Increment(Inc); // parameter is a variable cout << "The variable Inc is: "<<Inc<<endl; return 0; }
Image of page 4
2: Prog. Fundamentals: Pass by Reference/Slide 5 Pass by Reference: Example 2 It is possible to use both pass by reference and pass by value parameters in the same function. // Print the sum and average of two numbers // Input: two numbers num_1 and num_2 // Output: sum of num_1 and num_2 // average of num_1 and num_2 #include <iostream> using namespace std; void SumAve ( double , double , double &, double &);
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2: Prog. Fundamentals: Pass by Reference/Slide 6 Pass by Reference: Example 2 int main ( ) { double x, y, sum, mean; cout << "Enter two numbers: "; cin >> x >> y; SumAve (x, y, sum, mean); cout << "The sum is " << sum << endl; cout << "The average is " << mean << endl; return 0; } void SumAve( double no1, double no2, double & sum, double & average) { sum = no1 + no2; average = sum / 2; }
Image of page 6
2: Prog. Fundamentals: Pass by Reference/Slide 7 Pass by Reference: Example 2 Data areas after call to SumAve:
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2: Prog. Fundamentals: Pass by Reference/Slide 8
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the 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