passref

# passref - Programming Functions Passing Parameters by...

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

Programming Functions: Passing Parameters by Reference

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

View Full Document
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 &);
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.

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

View Full Document
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; }
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 &);

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

View Full Document
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; }
2: Prog. Fundamentals: Pass by Reference/Slide 7 Pass by Reference: Example 2 Data areas after call to SumAve:

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

View Full Document
2: Prog. Fundamentals: Pass by Reference/Slide 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern