Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions Pass by reference, or Call by reference Passing addresses Use of & part 3.

Similar presentations


Presentation on theme: "Functions Pass by reference, or Call by reference Passing addresses Use of & part 3."— Presentation transcript:

1

2 Functions Pass by reference, or Call by reference Passing addresses Use of & part 3

3 Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function prototype function call function definition

4 void main(void) {double height = 4.0, base = 3.0; && double Pythagorus(double &, double &); cout << “Hypotenuse = “ << Pythagorus(height, base) << endl;... } & & double Pythagorus(double& a, double& b) {double c; c = sqrt(a*a + b*b); return c; } Passing Data - by Reference - Example * address of height address of base c 5.0

5 & & double Pythagorus(double& a, double& b) {double c; a++; b++; c = sqrt(a*a + b*b); return c; } Passing Data - by Reference * address of height address of base back in main:cout << height; cout << base: 4.03.0 height base 5.0 4.0 6.4 c 5.0 4.0 Very Risky!

6 Passing Data - by Reference In main() values referenced as 1 value stored a height 1 value stored b base In Pythagorus() values referenced as *

7 Passing Data - by Reference { float a, b, c, sum, product; void calc(float, float, float, float&, float&); // prototype cout << "Enter three numbers: "; cin >> a >> b >> c; calc(a, b, c, sum, product); // call cout << a<<“ + “<<b<<“ + “c<<“ = " << sum; cout << ‘\n’<<a<<“ * “<<b<<“ * “c<<“ = " << product; } void calc(float x, float y, float z, float& tot, float& multiply) {tot = x + y + z; // definition multiply = x * y * z; x++; y++; z--;// for demo purposes }

8 Another Example – What Happens? calc(a, b, c, sum, product); void calc(float x, float y, float z, float& tot, float& multiply) {tot = x + y + z; multiply = x * y * z; x++; y++; z--; } 5 7 9 ->sum ? 5 7 9 ->product ? ab 6 csumproduct 88 21 315 product

9 Passing Data - by Reference Output Enter three numbers: 5 7 9 5 + 7 + 9 = 21 5 * 7 * 9 = 315 * x is 6, y is 8, z is 8 (only changed in function) (tot and sum refer to the same address product and multiply refer to the same address

10 Passing Data - by Reference void main(void) { int w = 3; void print_val(int &);// function prototype cout <<"w before the function call is "<<w<<‘\n’; print_val(w); cout <<"w after the function call is "<<w<<‘\n’; } void print_val(int& q) {cout<<"Value passed to the function is "<<q<<endl; q = q *2;// doubles the value of w? cout<<"Value at the end of the function is "<< q <<endl; }

11 Passing Data - by Reference Output w before the function call 3 Value passed to the function is 3 Value at the end of the function is 6 w after the function call is 6 Passing by reference is dangerous – especially in LARGE programs!! Void print_val (int &q); This function doubles the value of any int variable sent to it (not just q)

12 Swap Routine void swap(float& num1, float& num2) { float temp; temp = num1; num1 = num2; num2 = temp; } Now, here is perhaps a valid use! What happens if we use call by value for the function? Not much!

13 Data Type Mismatch value parameters implicit type conversion - value of the actual parameter is coerced to the data type of the formal parameter reference parameters no coercion because an address is passed, not a value *

14 A Comparison formalactual parameter isparameter may be valuevariable, constant, or expression type coercion may take place referencevariable only of exact same type as formal

15 What’s Happening???? call sequence 1. memory is allocated 2. parameters are passed 3. transfer of control return sequence 1. value of the return is stored 2. memory is deallocated 3. transfer of control *

16 Function Call Overhead Always uses the Call & Return sequence: When is it worth it? –when it simplifies the design –when overhead is negligible –when it abstracts a task –when it hides or protects data –when it consists of at least __ lines of code – Does it provide a net decrease in program size?

17 Drivers and Stubs Function Driver –when function is complete –A short “main” that calls the function and then tests its output –Written to test the validity of the prototype, the call, and the arguments Function Stub – when main is complete –A short function that just prints a message –Written to test the validity of the call, the function header, and the formal parameters


Download ppt "Functions Pass by reference, or Call by reference Passing addresses Use of & part 3."

Similar presentations


Ads by Google