computer science 1620 reference parameters. parameters – pass by value recall that the parameter...

93
Computer Science 1620 Reference Parameters

Post on 22-Dec-2015

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Computer Science 1620

Reference Parameters

Page 2: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Parameters – Pass by Value recall that the parameter of a function is

assigned the value of its corresponding argument in the function call

#include <iostream> using namespace std;

double factorial(int x) { double result = 1; for (int y = 1; y <= x; y++) result *= y; return result;}

int main() { cout << "5! = " << factorial(5) << endl; return 0;}

• Before function starts to execute, x is assigned the value of its corresponding argument. • It is as though an implicit x=5 exists in the code before the function starts

Page 3: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Parameters – Pass by Valuewhat about when a variable is usede.g. what is the output of this code?

#include <iostream> using namespace std;

void change(int x) { x = 2;}

int main() { int y = 4; change(y); cout << "y = " << y << endl; return 0;}

Page 4: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Parameters – Pass By Valuewhy didn't function change y's value to 2

the parameter only receives a copy of x's value, not the actual value itself

the same reason that this code outputs 4#include <iostream> using namespace std;int main() { int y = 4; int x = y; x = 2; cout << "y = " << y << endl; return 0;}

Page 5: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Program Memory:Program:

Output:

#include <iostream> using namespace std;

void change(int x) { x = 2;}

int main() { int y = 4; change(y); cout << "y = " << y << endl; return 0;}

Page 6: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Program Memory:Program:

Output:

#include <iostream> using namespace std;

void change(int x) { x = 2;}

int main() { int y = 4; change(y); cout << "y = " << y << endl; return 0;}

int y = 4;

main: y 4

Page 7: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Program Memory:Program:

Output:

#include <iostream> using namespace std;

void change(int x) { x = 2;}

void main() { int y = 4; change(y); cout << "y = " << y << endl; return 0;}

change(y);

main: y 4

change: x 4

Page 8: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Program Memory:Program:

Output:

#include <iostream> using namespace std;

void change(int x) { x = 2;}

int main() { int y = 4; change(y); cout << "y = " << y << endl; return 0;}

x = 2;

main: y 4

change: x 42

Page 9: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Program Memory:Program:

Output:

#include <iostream> using namespace std;

void change(int x) { x = 2;}

int main() { int y = 4; change(y); cout << "y = " << y << endl; return 0;}

main: y 4

change: x 42

Page 10: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Program Memory:Program:

Output:

#include <iostream> using namespace std;

void change(int x) { x = 2;}

int main() { int y = 4; change(y); cout << "y = " << y << endl; return 0;}

main: y 4

change: x 42

cout << "y = " << y << endl;

4

Page 11: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Program Memory:Program:

Output:

#include <iostream> using namespace std;

void change(int x) { x = 2;}

int main() { int y = 4; change(y); cout << "y = " << y << endl; return 0;}

main: y 4

change: x 42

return 0;

4

Page 12: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Pass By Valuewhen a parameter receives only a copy of

its argument value, this is called pass by value

all of our examples so far have followed this paradigm

Page 13: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Functions (using pass by value) three limitations

1) Cases exist where changing variable would be valuable

2) Making copies of data is sometimes expensive and unnecessary

3) We can currently only return one value from a function

Page 14: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Functions (using pass by value)Limitation 1: Cases exist where changing

the value of the variable would be usefulExample: write a function that swaps the values

of two variables (very useful for sorting)

Page 15: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream> using namespace std;

void swap(int a, int b) { int temp = b; b = a; a = temp;}

int main() {

int x = 3; int y = 4;

cout << "x = " << x << ", y = " << y << endl; swap(x, y);

cout << "x = " << x << ", y = " << y << endl;

return 0;

}

Page 16: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Functions (using pass by value)Limitation 2: Making copies of the data is

unnecessary and sometimes expensiveExample: write a function that takes a name,

city, and province as inputs, and writes a short bio of the person.

Page 17: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream>#include <string> using namespace std;

void bio(string name, string city, string province) { cout << name << " currently lives in " << city << ", " << province << endl;}

int main() {

string name = "Kevin"; string city = "Lethbridge"; string province = "Alberta";

bio(name, city, province);

return 0;

}

Each string gets two copies in memory• one for main• one for bio

Two copies is unnecessary, since bio never changes these strings.

Page 18: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Functions (using pass by value)Limitation 3: Functions can only return one

valueExample: write a function that takes the radius

of a circle as its input, and returns its area and its circumference

Page 19: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream> using namespace std;

const double PI = 3.14159;

??? area_and_circumference(double radius) {

double area = PI * radius * radius; double circumference = 2 * PI * radius;

return ?????;

}

Page 20: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

What do we do when pass by value doesn't work?One possible solution: global variables

e.g. consider our last problem (two return values)

we could simply have two global variables to store the results of our computation

Page 21: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream> using namespace std;

const double PI = 3.14159;

double area;double circumference;

void area_and_circumference(double radius) {

area = PI * radius * radius; circumference = 2 * PI * radius;

return ?????;

}

int main() {

area_and_circumference(5); cout << "Area = " << area << endl; cout << "Circumference = " << circumference << endl; return 0;}

Page 22: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

The previous slide worksbut we know that global variables are not

recommended for passing information between functions

Is there a way to avoid the limitations of pass-by-value, without resorting to global variables?Yes: pass-by-reference

Page 23: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Reference Variablessimilar to a standard variableshares its memory with another variable

type name

The left side of the assignment operator looks exactly like a normal variable declaration – with the inclusion of an & before the variable name.

The reference variable is set to another variable. The variable var and the reference variable name now share the same memory.

& = var;

Page 24: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Reference Variablesonce a reference variable is declared, you

use it just like any other variableyou only need the & at declaration

that is, you can use it in an expressionon the left side of assignmentetc.

Page 25: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Example

#include <iostream> using namespace std;

int main() { int y = 4; int x = y; x = 2; cout << "y = " << y << endl; return 0;}

What happens if we makex a reference variable?

Page 26: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Example

#include <iostream> using namespace std;

int main() { int y = 4; int &x = y; x = 2; cout << "y = " << y << endl; return 0;}

What happens if we makex a reference variable?

Page 27: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Program Memory:Program:

Output:

#include <iostream> using namespace std;

int main() {

int y = 4; int &x = y; x = 2; cout << "y = " << y << endl;

return 0;

}

Page 28: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Program Memory:Program:

Output:

#include <iostream> using namespace std;

int main() {

int y = 4; int &x = y; x = 2; cout << "y = " << y << endl;

return 0;

}

main: y 4

int y = 4;

Page 29: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Program Memory:Program:

Output:

#include <iostream> using namespace std;

int main() {

int y = 4; int &x = y; x = 2; cout << "y = " << y << endl;

return 0;

}

main: y 4

int &x = y;

main: x

Because x is a reference variable that is set equal to y, it now shares y's memory.

Page 30: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Program Memory:Program:

Output:

#include <iostream> using namespace std;

int main() {

int y = 4; int &x = y; x = 2; cout << "y = " << y << endl;

return 0;

}

main: y 2

x = 2;

main: x

Page 31: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Program Memory:Program:

Output:

#include <iostream> using namespace std;

int main() {

int y = 4; int &x = y; x = 2; cout << "y = " << y << endl;

return 0;

}

main: y 2

cout << "y = " << y << endl;

main: x

2

Page 32: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Reference VariablesSome rules:

when declaring a reference variable in a function, you must set it to another variable at declaration

int &x; // this is a compiler erroronce a reference variable is set, you cannot set

it to another variable int a, b; int &x = a; &x = b; // this is a compiler error

Page 33: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Reference Parameters in practice, reference variables are rarely

declared inside a function instead, they are used as function

parameters these are referred to as reference

parameters reference parameters allow us to pass-by-

reference

Page 34: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream> using namespace std;

void swap(int a, int b) { int temp = b; b = a; a = temp;}

int main() {

int x = 3; int y = 4;

cout << "x = " << x << ", y = " << y << endl; swap(x, y);

cout << "x = " << x << ", y = " << y << endl;

return 0;

}

Example: Write a function called swap that swaps the values of two variables.

What happens if we makea and b reference parameters?

Page 35: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream> using namespace std;

void swap(int &a, int &b) { int temp = b; b = a; a = temp;}

int main() {

int x = 3; int y = 4;

cout << "x = " << x << ", y = " << y << endl; swap(x, y);

cout << "x = " << x << ", y = " << y << endl;

return 0;

}

Example: Write a function called swap that swaps the values of two variables.

What happens if we makea and b reference parameters?

Page 36: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Program Memory:Program:

Output:

cout << "y = " << y << endl;

void swap(int &a, int &b) { int temp = b; b = a; a = temp;}

int main() {

int x = 3; int y = 4;

cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "x = " << x << ", y = " << y << endl; return 0;}

Page 37: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Program Memory:Program:

Output:

cout << "y = " << y << endl;

void swap(int &a, int &b) { int temp = b; b = a; a = temp;}

int main() {

int x = 3; int y = 4;

cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "x = " << x << ", y = " << y << endl; return 0;}

int x = 3;

main: x 3

Page 38: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Program Memory:Program:

Output:

cout << "y = " << y << endl;

void swap(int &a, int &b) { int temp = b; b = a; a = temp;}

int main() {

int x = 3; int y = 4;

cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "x = " << x << ", y = " << y << endl; return 0;}

int y = 4;

main: x 3

main: y 4

Page 39: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Program Memory:Program:

Output:

cout << "y = " << y << endl;

void swap(int &a, int &b) { int temp = b; b = a; a = temp;}

int main() {

int x = 3; int y = 4;

cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "x = " << x << ", y = " << y << endl; return 0;}

cout << "x = " << x << ", y = " << y << endl;

main: x 3

main: y 4

x = 3, y = 4

Page 40: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Program Memory:Program:

Output:

cout << "y = " << y << endl;

void swap(int &a, int &b) { int temp = b; b = a; a = temp;}

int main() {

int x = 3; int y = 4;

cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "x = " << x << ", y = " << y << endl; return 0;}

swap(x, y);

main: x 3

main: y 4

x = 3, y = 4

swap: a

swap: b

Page 41: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Program Memory:Program:

Output:

cout << "y = " << y << endl;

void swap(int &a, int &b) { int temp = b; b = a; a = temp;}

int main() {

int x = 3; int y = 4;

cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "x = " << x << ", y = " << y << endl; return 0;}

int temp = b;main: x 3

main: y 4

x = 3, y = 4

swap: a

swap: b

swap: temp

4

Page 42: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

4

Program Memory:Program:

Output:

cout << "y = " << y << endl;

void swap(int &a, int &b) { int temp = b; b = a; a = temp;}

int main() {

int x = 3; int y = 4;

cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "x = " << x << ", y = " << y << endl; return 0;}

b = a;

main: x 3

main: y

x = 3, y = 4

swap: a

swap: b

swap: temp

4

3

Page 43: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

4

Program Memory:Program:

Output:

cout << "y = " << y << endl;

void swap(int &a, int &b) { int temp = b; b = a; a = temp;}

int main() {

int x = 3; int y = 4;

cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "x = " << x << ", y = " << y << endl; return 0;}

a = temp;

main: x 3

main: y

x = 3, y = 4

swap: a

swap: b

swap: temp

4

3

4

Page 44: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

4

Program Memory:Program:

Output:

cout << "y = " << y << endl;

void swap(int &a, int &b) { int temp = b; b = a; a = temp;}

int main() {

int x = 3; int y = 4;

cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "x = " << x << ", y = " << y << endl; return 0;}

main: x 3

main: y

x = 3, y = 4

swap: a

swap: b

swap: temp

4

3

4

Page 45: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

4

Program Memory:Program:

Output:

cout << "y = " << y << endl;

void swap(int &a, int &b) { int temp = b; b = a; a = temp;}

int main() {

int x = 3; int y = 4;

cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "x = " << x << ", y = " << y << endl; return 0;}

main: x 3

main: y

x = 3, y = 4

4

3

4

cout << "x = " << x << ", y = " << y << endl;

x = 4, y = 3

Page 46: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Reference Parameterswhen a variable is passed by reference,

changes to the value of the parameter affect the corresponding argument variable

e.g. last examplewhat else do reference parameters give us?

more efficient parameter passing

Page 47: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream>#include <string> using namespace std;

void bio(string name, string city, string province) { cout << name << " currently lives in " << city << ", " << province << endl;}

int main() {

string name = "Kevin"; string city = "Lethbridge"; string province = "Alberta";

bio(name, city, province);

return 0;

}

write a function that takes a name, city, and province as inputs, and writes a short bio of the person.

Each string gets two copies in memory• one for main• one for bio

What if we made the parameters reference parameters?

Page 48: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream>#include <string> using namespace std;

void bio(string &name, string &city, string &province) { cout << name << " currently lives in " << city << ", " << province << endl;}

int main() {

string name = "Kevin"; string city = "Lethbridge"; string province = "Alberta";

bio(name, city, province);

return 0;

}

write a function that takes a name, city, and province as inputs, and writes a short bio of the person.

Now each string only gets one copy in memory• the parameter and argument variable refer to the same memory

Page 49: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Receiving multiple values from a function reference parameters can be used to "receive" more than

one value from a function Steps:

1. for each value to be returned, the function will declare a reference parameter

2. the calling function will send a variable to each of these reference parameters

3. the called function will assign each return value to one of these reference parameters

4. when the function ends, the return values will be in the variables that were sent

Page 50: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream> using namespace std;

const double PI = 3.14159;

area_and_circumference(double radius) {

}

int main() {

double r = 5.0; // radius of circle

return 0;}

Write a function that takes a radius of a circle, and returns the area and circumference of that circle.

1. for each value to be returned, the function will declare a reference parameter

2. the calling function will send a variable to each of these reference parameters

3. the called function will assign each return value to one of these reference parameters

4. when the function ends, the return values will be in the variables that were sent

Page 51: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream> using namespace std;

const double PI = 3.14159;

area_and_circumference(double radius) {

}

int main() {

double r = 5.0; // radius of circle

return 0;}

Write a function that takes a radius of a circle, and returns the area and circumference of that circle.

1. for each value to be returned, the function will declare a reference parameter

2. the calling function will send a variable to each of these reference parameters

3. the called function will assign each return value to one of these reference parameters

4. when the function ends, the return values will be in the variables that were sent

Page 52: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream> using namespace std;

const double PI = 3.14159;

area_and_circumference(double radius, double &area, double &circumference) {

}

int main() {

double r = 5.0; // radius of circle

return 0;}

Write a function that takes a radius of a circle, and returns the area and circumference of that circle.

1. for each value to be returned, the function will declare a reference parameter

2. the calling function will send a variable to each of these reference parameters

3. the called function will assign each return value to one of these reference parameters

4. when the function ends, the return values will be in the variables that were sent

Page 53: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream> using namespace std;

const double PI = 3.14159;

area_and_circumference(double radius, double &area, double &circumference) {

}

int main() {

double r = 5.0; // radius of circle

return 0;}

Write a function that takes a radius of a circle, and returns the area and circumference of that circle.

1. for each value to be returned, the function will declare a reference parameter

2. the calling function will send a variable to each of these reference parameters

3. the called function will assign each return value to one of these reference parameters

4. when the function ends, the return values will be in the variables that were sent

Page 54: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream> using namespace std;

const double PI = 3.14159;

area_and_circumference(double radius, double &area, double &circumference) {

}

int main() {

double r = 5.0; // radius of circle

double a, c;

area_and_circumference(r, a, c);

return 0;}

Write a function that takes a radius of a circle, and returns the area and circumference of that circle.

1. for each value to be returned, the function will declare a reference parameter

2. the calling function will send a variable to each of these reference parameters

3. the called function will assign each return value to one of these reference parameters

4. when the function ends, the return values will be in the variables that were sent

Page 55: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream> using namespace std;

const double PI = 3.14159;

area_and_circumference(double radius, double &area, double &circumference) {

}

int main() {

double r = 5.0; // radius of circle

double a, c;

area_and_circumference(r, a, c);

return 0;}

Write a function that takes a radius of a circle, and returns the area and circumference of that circle.

1. for each value to be returned, the function will declare a reference parameter

2. the calling function will send a variable to each of these reference parameters

3. the called function will assign each return value to one of these reference parameters

4. when the function ends, the return values will be in the variables that were sent

Page 56: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream> using namespace std;

const double PI = 3.14159;

area_and_circumference(double radius, double &area, double &circumference) { area = PI * radius * radius; circumference = 2 * PI * radius;

}

int main() {

double r = 5.0; // radius of circle

double a, c;

area_and_circumference(r, a, c);

return 0;}

Write a function that takes a radius of a circle, and returns the area and circumference of that circle.

1. for each value to be returned, the function will declare a reference parameter

2. the calling function will send a variable to each of these reference parameters

3. the called function will assign each return value to one of these reference parameters

4. when the function ends, the return values will be in the variables that were sent

Page 57: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream> using namespace std;

const double PI = 3.14159;

area_and_circumference(double radius, double &area, double &circumference) { area = PI * radius * radius; circumference = 2 * PI * radius;

}

int main() {

double r = 5.0; // radius of circle

double a, c;

area_and_circumference(r, a, c);

return 0;}

Write a function that takes a radius of a circle, and returns the area and circumference of that circle.

1. for each value to be returned, the function will declare a reference parameter

2. the calling function will send a variable to each of these reference parameters

3. the called function will assign each return value to one of these reference parameters

4. when the function ends, the return values will be in the variables that were sent

Page 58: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream> using namespace std;

const double PI = 3.14159;

area_and_circumference(double radius, double &area, double &circumference) { area = PI * radius * radius; circumference = 2 * PI * radius;

}

int main() {

double r = 5.0; // radius of circle

double a, c;

area_and_circumference(r, a, c);

cout << "Area = " << a << endl; cout << "Circumference = " << c << endl;

return 0;}

Write a function that takes a radius of a circle, and returns the area and circumference of that circle.

1. for each value to be returned, the function will declare a reference parameter

2. the calling function will send a variable to each of these reference parameters

3. the called function will assign each return value to one of these reference parameters

4. when the function ends, the return values will be in the variables that were sent

Page 59: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Reference Parameterswhat is the return type of the previous

program?since no value is being "returned" through a

return statement, it's return type is void

Page 60: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream> using namespace std;

const double PI = 3.14159;

void area_and_circumference(double radius, double &area, double &circumference) { area = PI * radius * radius; circumference = 2 * PI * radius;

}

int main() {

double r = 5.0; // radius of circle

double a, c;

area_and_circumference(r, a, c);

cout << "Area = " << a << endl; cout << "Circumference = " << c << endl;

return 0;}

Write a function that takes a radius of a circle, and returns the area and circumference of that circle.

Page 61: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Reference Parameters and Return Values if your function returns more than one value, you

can use both the return value and reference parameters

return one value through a return statement return the rest of the values through reference parameters e.g. rewrite the previous program, but return the area

through a return statement, and the circumference through a reference parameter

Page 62: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream> using namespace std;

const double PI = 3.14159;

void area_and_circumference(double radius, double &area, double &circumference) { area = PI * radius * radius; circumference = 2 * PI * radius;

}

int main() {

double r = 5.0; // radius of circle

double a, c;

area_and_circumference(r, a, c);

cout << "Area = " << a << endl; cout << "Circumference = " << c << endl;

return 0;}

Write a function that takes a radius of a circle, and returns the area and circumference of that circle.

Page 63: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream> using namespace std;

const double PI = 3.14159;

void area_and_circumference(double radius, double &area, double &circumference) { area = PI * radius * radius; circumference = 2 * PI * radius;

}

int main() {

double r = 5.0; // radius of circle

double a, c;

area_and_circumference(r, a, c);

cout << "Area = " << a << endl; cout << "Circumference = " << c << endl;

return 0;}

Write a function that takes a radius of a circle, and returns the area and circumference of that circle.

Since area will be returned using a return statement, we do not need to use a reference parameter for this value.

Page 64: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream> using namespace std;

const double PI = 3.14159;

void area_and_circumference(double radius, double &circumference) { area = PI * radius * radius; circumference = 2 * PI * radius;

}

int main() {

double r = 5.0; // radius of circle

double a, c;

area_and_circumference(r, a, c);

cout << "Area = " << a << endl; cout << "Circumference = " << c << endl;

return 0;}

Write a function that takes a radius of a circle, and returns the area and circumference of that circle.

Page 65: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream> using namespace std;

const double PI = 3.14159;

void area_and_circumference(double radius, double &circumference) { area = PI * radius * radius; circumference = 2 * PI * radius;

}

int main() {

double r = 5.0; // radius of circle

double a, c;

area_and_circumference(r, a, c);

cout << "Area = " << a << endl; cout << "Circumference = " << c << endl;

return 0;}

Write a function that takes a radius of a circle, and returns the area and circumference of that circle.

Since area will be returned using a return statement, we need to declare what type this return value will be.

Page 66: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream> using namespace std;

const double PI = 3.14159;

double area_and_circumference(double radius, double &circumference) { area = PI * radius * radius; circumference = 2 * PI * radius;

}

int main() {

double r = 5.0; // radius of circle

double a, c;

area_and_circumference(r, a, c);

cout << "Area = " << a << endl; cout << "Circumference = " << c << endl;

return 0;}

Write a function that takes a radius of a circle, and returns the area and circumference of that circle.

Page 67: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream> using namespace std;

const double PI = 3.14159;

double area_and_circumference(double radius, double &circumference) { area = PI * radius * radius; circumference = 2 * PI * radius;

}

int main() {

double r = 5.0; // radius of circle

double a, c;

area_and_circumference(r, a, c);

cout << "Area = " << a << endl; cout << "Circumference = " << c << endl;

return 0;}

Write a function that takes a radius of a circle, and returns the area and circumference of that circle.

This value should be returned, not stored in a variable.

Page 68: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream> using namespace std;

const double PI = 3.14159;

double area_and_circumference(double radius, double &circumference) { circumference = 2 * PI * radius; return PI * radius * radius;}

int main() {

double r = 5.0; // radius of circle

double a, c;

area_and_circumference(r, a, c);

cout << "Area = " << a << endl; cout << "Circumference = " << c << endl;

return 0;}

Write a function that takes a radius of a circle, and returns the area and circumference of that circle.

Page 69: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream> using namespace std;

const double PI = 3.14159;

double area_and_circumference(double radius, double &circumference) { circumference = 2 * PI * radius; return PI * radius * radius;}

int main() {

double r = 5.0; // radius of circle

double a, c;

area_and_circumference(r, a, c);

cout << "Area = " << a << endl; cout << "Circumference = " << c << endl;

return 0;}

Write a function that takes a radius of a circle, and returns the area and circumference of that circle.

The area of the circle is not returned through a reference parameter, but is the value of the function call.

Page 70: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

#include <iostream> using namespace std;

const double PI = 3.14159;

double area_and_circumference(double radius, double &circumference) { circumference = 2 * PI * radius; return PI * radius * radius;}

int main() {

double r = 5.0; // radius of circle

double a, c;

a = area_and_circumference(r, c);

cout << "Area = " << a << endl; cout << "Circumference = " << c << endl;

return 0;}

Write a function that takes a radius of a circle, and returns the area and circumference of that circle.

Page 71: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Reference Parameters and Return Values When my function returns a value, should I use a

return statement, or reference parameters? if your function returns only one value, a return statement

is typically used if your function returns more than one value, you need to

use a reference parameter if you wish, you can use both

Page 72: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Another Examplewrite a function that receives two numbers A

and B, and returns A / B in division format. That is, it returns the quotient and the remainder.

e.g., if A = 5, and B = 2, the function should return 2 as the quotient, and 1 as the remainder

Page 73: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Design (function):Step 1: Compute the quotient of A / BStep 2: Compute the remainder of A / BStep 3: Return these two values

Page 74: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

divide(int A, int B, ) {

Step 1: Compute the quotient of A / BStep 2: Compute the remainder of A / BStep 3: Return these two values

}

Page 75: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

divide(int A, int B, ) {

Step 1: Compute the quotient of A / BStep 2: Compute the remainder of A / BStep 3: Return these two values

}

Page 76: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

divide(int A, int B, ) {

int quotient = A / B;Step 2: Compute the remainder of A / BStep 3: Return these two values

}

Page 77: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

divide(int A, int B, ) {

int quotient = A / B;Step 2: Compute the remainder of A / BStep 3: Return these two values

}

Page 78: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

divide(int A, int B, ) {

int quotient = A / B;

int remainder = A % B;Step 3: Return these two values

}

Page 79: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

divide(int A, int B, ) {

int quotient = A / B;

int remainder = A % B;Step 3: Return these two values

}

Page 80: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

divide(int A, int B, ) {

int quotient = A / B;

int remainder = A % B;Step 3.1: Return quotientStep 3.2: Return remainder

}

Page 81: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

How do we return these two values: two choices

Choice 1: return them both as reference parameters

Choice 2: return one as the return value, and one as a reference parameter

Page 82: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

void divide(int A, int B, ) {

int quotient = A / B;

int remainder = A % B;Step 3.1: Return quotient as ref. paramStep 3.2: Return remainder as ref. param

}

Page 83: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

void divide(int A, int B,int &qparam ){

int quotient = A / B;

int remainder = A % B;

qparam = quotient;Step 3.2: Return remainder as ref. param

}

Page 84: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

void divide(int A, int B, int &qparam ){

int quotient = A / B;

int remainder = A % B;

qparam = quotient;Step 3.2: Return remainder as ref. param

}

Page 85: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

void divide(int A, int B, int &qparam, int &rparam){

int quotient = A / B;

int remainder = A % B;

qparam = quotient;

rparam = remainder;

}

Page 86: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

void divide(int A, int B, int &qparam, int &rparam){

int qparam = A / B;

int rparam = A % B;

}

Page 87: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

How do we return these two values: two choices

Choice 1: return them both as reference parameters

Choice 2: return one as the return value, and one as a reference parameter

Page 88: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

divide(int A, int B, ) {

int quotient = A / B;

int remainder = A % B;Step 3.1: Return quotient as ref. paramStep 3.2: Return remainder as ret. value

}

Page 89: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

divide(int A, int B, int &qparam) {

int quotient = A / B;

int remainder = A % B;

qparam = quotient;Step 3.2: Return remainder as ret. value

}

Page 90: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

divide(int A, int B, int &qparam) {

int quotient = A / B;

int remainder = A % B;

qparam = quotient;Step 3.2: Return remainder as ret. value

}

Page 91: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

int divide(int A, int B, int &qparam) {

int quotient = A / B;

int remainder = A % B;

qparam = quotient;

return remainder;

}

Page 92: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

int divide(int A, int B, int &qparam) {

qparam = A / B;

return A % B;

}

Page 93: Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding

Summary reference variables are variables that share

memory with another variable that's already declared

Advantages of reference parameters:allow a function to change the state of a variable

outside of that function improve efficiency by avoiding duplicationallow multiple return values from a function