passref - Programming Functions: Passing Parameters by...

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

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

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

View Full DocumentRight 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 &);
Background 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.
Background image of page 3

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

View Full DocumentRight 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; }
Background 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 &);
Background image of page 5

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

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

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

View Full DocumentRight Arrow Icon
2: Prog. Fundamentals: Pass by Reference/Slide 8
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 21

passref - Programming Functions: Passing Parameters by...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online