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

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

Parameter Passing Mechanisms Reference Parameters.
Introduction to Programming (in C++) Subprograms: procedures and functions Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science,
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Computer Science 1620 Loops.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Programming Scope of Identifiers. COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 2 Scope l A sequence of statements within { … } is considered.
Computer Science 1620 Arithmetic. C++ Math we have seen how to use numbers in our program to represent data however, we can also manipulate this data.
Computer Programming 1 More on functions. Computer Programming 2 Objectives Function overloading Scope rules and namespace Inline Templates Pass by value.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Computer Science 1620 Function Scope & Global Variables.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Pass by Reference. COMP104 Pass by Reference / Slide 2 Passing Parameters by Reference * To have a function with multiple outputs, we have to use pass.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Computer Science 1620 Selection Structures. Write a program that accepts the speed of a vehicle, and determines whether that person is speeding assume.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Chapter 6: Functions.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Programming Functions: Passing Parameters by Reference.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
CPS120: Introduction to Computer Science Functions.
1 CS161 Introduction to Computer Science Topic #10.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
PHY 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
CMSC 202 Lesson 6 Functions II. Warmup Correctly implement a swap function such that the following code will work: int a = 7; int b = 8; Swap(a, b); cout.
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value –A copy of the arguments’svalue.
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
Parameter passing Jordi Cortadella Department of Computer Science.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
A Sample Program #include using namespace std; int main(void) { cout
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
User-Written Functions
Chapter 5 Function Basics
A Lecture for the c++ Course
Pointers and Pointer-Based Strings
Functions Inputs Output
Object Oriented Programming COP3330 / CGS5409
Pointers & Functions.
Chapter 4 Implementing Free Functions
Functions Pass By Value Pass by Reference
Function “Inputs and Outputs”
Pointers and Pointer-Based Strings
Pointers & Functions.
Presentation transcript:

Computer Science 1620 Reference Parameters

Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding argument in the function call #include 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

Parameters – Pass by Value what about when a variable is used e.g. what is the output of this code? #include using namespace std; void change(int x) { x = 2; } int main() { int y = 4; change(y); cout << "y = " << y << endl; return 0; }

Parameters – Pass By Value why 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 using namespace std; int main() { int y = 4; int x = y; x = 2; cout << "y = " << y << endl; return 0; }

Program Memory: Program: Output: #include using namespace std; void change(int x) { x = 2; } int main() { int y = 4; change(y); cout << "y = " << y << endl; return 0; }

Program Memory: Program: Output: #include 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

Program Memory: Program: Output: #include 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

Program Memory: Program: Output: #include 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

Program Memory: Program: Output: #include 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

Program Memory: Program: Output: #include 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

Program Memory: Program: Output: #include 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

Pass By Value when 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

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

Functions (using pass by value) Limitation 1: Cases exist where changing the value of the variable would be useful Example: write a function that swaps the values of two variables (very useful for sorting)

#include 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; }

Functions (using pass by value) Limitation 2: Making copies of the data is unnecessary and sometimes expensive Example: write a function that takes a name, city, and province as inputs, and writes a short bio of the person.

#include 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.

Functions (using pass by value) Limitation 3: Functions can only return one value Example: write a function that takes the radius of a circle as its input, and returns its area and its circumference

#include using namespace std; const double PI = ; ??? area_and_circumference(double radius) { double area = PI * radius * radius; double circumference = 2 * PI * radius; return ?????; }

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

#include using namespace std; const double PI = ; 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; }

The previous slide works but 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

Reference Variables similar to a standard variable shares its memory with another variable typename 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;

Reference Variables once a reference variable is declared, you use it just like any other variable you only need the & at declaration that is, you can use it in an expression on the left side of assignment etc.

Example #include using namespace std; int main() { int y = 4; int x = y; x = 2; cout << "y = " << y << endl; return 0; } What happens if we make x a reference variable?

Example #include using namespace std; int main() { int y = 4; int &x = y; x = 2; cout << "y = " << y << endl; return 0; } What happens if we make x a reference variable?

Program Memory: Program: Output: #include using namespace std; int main() { int y = 4; int &x = y; x = 2; cout << "y = " << y << endl; return 0; }

Program Memory: Program: Output: #include 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;

Program Memory: Program: Output: #include 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.

Program Memory: Program: Output: #include 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

Program Memory: Program: Output: #include 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

Reference Variables Some rules: when declaring a reference variable in a function, you must set it to another variable at declaration int &x; // this is a compiler error once 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

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

#include 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 make a and b reference parameters?

#include 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 make a and b reference parameters?

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; }

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

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

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

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

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

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

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

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

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 = cout << "x = " << x << ", y = " << y << endl; x = 4, y = 3

Reference Parameters when a variable is passed by reference, changes to the value of the parameter affect the corresponding argument variable e.g. last example what else do reference parameters give us? more efficient parameter passing

#include 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?

#include 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

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

#include using namespace std; const double PI = ; 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

#include using namespace std; const double PI = ; 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

#include using namespace std; const double PI = ; 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

#include using namespace std; const double PI = ; 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

#include using namespace std; const double PI = ; 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

#include using namespace std; const double PI = ; 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

#include using namespace std; const double PI = ; 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

#include using namespace std; const double PI = ; 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

#include using namespace std; const double PI = ; 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

Reference Parameters what is the return type of the previous program? since no value is being "returned" through a return statement, it's return type is void

#include using namespace std; const double PI = ; 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.

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

#include using namespace std; const double PI = ; 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.

#include using namespace std; const double PI = ; 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.

#include using namespace std; const double PI = ; 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.

#include using namespace std; const double PI = ; 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.

#include using namespace std; const double PI = ; 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.

#include using namespace std; const double PI = ; 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.

#include using namespace std; const double PI = ; 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.

#include using namespace std; const double PI = ; 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.

#include using namespace std; const double PI = ; 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.

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

Another Example write 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

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

divide(int A, int B, ) { Step 1: Compute the quotient of A / B Step 2: Compute the remainder of A / B Step 3: Return these two values }

divide(int A, int B, ) { Step 1: Compute the quotient of A / B Step 2: Compute the remainder of A / B Step 3: Return these two values }

divide(int A, int B, ) { int quotient = A / B; Step 2: Compute the remainder of A / B Step 3: Return these two values }

divide(int A, int B, ) { int quotient = A / B; Step 2: Compute the remainder of A / B Step 3: Return these two values }

divide(int A, int B, ) { int quotient = A / B; int remainder = A % B; Step 3: Return these two values }

divide(int A, int B, ) { int quotient = A / B; int remainder = A % B; Step 3: Return these two values }

divide(int A, int B, ) { int quotient = A / B; int remainder = A % B; Step 3.1: Return quotient Step 3.2: Return remainder }

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

void divide(int A, int B, ) { int quotient = A / B; int remainder = A % B; Step 3.1: Return quotient as ref. param Step 3.2: Return remainder as ref. param }

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 }

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 }

void divide(int A, int B, int &qparam, int &rparam){ int quotient = A / B; int remainder = A % B; qparam = quotient; rparam = remainder; }

void divide(int A, int B, int &qparam, int &rparam){ int qparam = A / B; int rparam = A % B; }

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

divide(int A, int B, ) { int quotient = A / B; int remainder = A % B; Step 3.1: Return quotient as ref. param Step 3.2: Return remainder as ret. value }

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 }

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 }

int divide(int A, int B, int &qparam) { int quotient = A / B; int remainder = A % B; qparam = quotient; return remainder; }

int divide(int A, int B, int &qparam) { qparam = A / B; return A % B; }

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 duplication allow multiple return values from a function