passing data - by reference syntax && double pythagorus(double &, double &);...

13
Passing Data - by Passing Data - by Reference Reference Syntax double Pythagorus(double & &, double & &); Pythagorus(height, base); double Pythagorus(double & & a, double & & b) function prototype function call function definition Addres s operat or

Upload: hilary-lane

Post on 04-Jan-2016

232 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function

Passing Data - by ReferencePassing Data - by Reference

Syntax

double Pythagorus(double &&, double &&);

Pythagorus(height, base);

double Pythagorus(double& & a, double& & b)

function prototype

function call

function definition

Addressoperator

Page 2: Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function

double Pythagorus(double &&, double &&);void main(void){ double height = 4.0, base = 3.0;

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 ExamplePassing Data by-reference Example

* *

addressaddress of heightof height

address address of baseof base

5.0

c

Page 3: Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function

double Pythagorus(double& & a, double& & b){ double c;

a++;b++;

c = sqrt(a*a + b*b);return c;

}

Passing Data - by ReferencePassing Data - by Reference

* * * * *

addressaddress of heightof height

address address of baseof base

back in main: cout << height;cout << base:

4.0 3.0

c

5.0 4.0

6.4

6.4

5.04.0

height base

Page 4: Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function

Passing Data - by ReferencePassing Data - by Reference

In main() valuesreferenced as

1 value stored1 value stored

a

height

1 value stored1 value stored

b

base

In Pythagorus()values referenced as

*

Page 5: Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function

By-Reference, Another ExampleBy-Reference, Another Example{

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); // callcout << 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

}

Page 6: Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function

Another Example: What happens?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 ? ?a b c sum product

5 7 9 sum product

21 315

6 8 8

Page 7: Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function

Another Example: Program OutputAnother Example: Program Output

OutputEnter three numbers: 5 7 95 + 7 + 9 = 215 * 7 * 9 = 315

*

x is 6, y is 8, z is 8tot and sum refer to the same addressproduct and multiply refer to the same address

Page 8: Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function

Passing Data - by ReferencePassing Data - by Referencevoid main(void){ int w = 3;

void print_val(int &); // local 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;

}

Page 9: Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function

Passing Data - by ReferencePassing 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

void print_val (int &q);

This function doubles the value of any (int) variable sent to it (not just w).

Page 10: Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function

Swap RoutineSwap Routine

void swap(float& num1, float& num2){

float temp;

temp = num1;num1 = num2;num2 = temp;

}

What happens if we use call by-

value for the swap function?

Page 11: Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function

Data Type MismatchData Type Mismatchvalue parameters

implicit type conversion - value of the actual parameter is coerced to the data type of the formal parameter

reference parameterscoercion not possible because an address is passed, not a value- types must match

* *

Page 12: Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function

A ComparisonA Comparison

formal actualparameter is parameter may be

value variable, constant, or expression

type coercion may take place

reference variable onlyof exact same type as formal

Page 13: Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function

What’s Happening????What’s Happening????call sequence

1. memory is allocated 2. parameters are passed3. transfer of control

return sequence

1. value of the return is stored 2. memory is deallocated3. transfer of control

* *