Lecture 04: Pass by reference

Jan 22, 2024  β”‚  Last updated Jan 21, 2024 by Charlotte Curtis

HTML Slides html β”‚ PDF Slides PDF

Where we left off

#include <iomanip>
cout.precision(2);
cout << fixed;
cout << "Total: $" << setw(8) 
     << bill << endl;
cout << "With GST: $" << setw(8) 
     << bill*1.05 << endl;

Textbook Sections 4.1-4.5

Today’s topics

Textbook Sections 4.5, 5.1-5.5

Variable scope

Function parameters are local variables

Scope guidelines

emoji Functions check-in 1/2

Based on the following function prototype (declaration), which of the following is a valid function call?

double compute_interest(double balance, double rate, int years);
  1. int interest = compute_interest(1000, 0.05, 3);
  2. compute_interest(1000, 0.05, 0.5);
  3. double interest = compute_interest(1000, 0.05, 3);
  4. cout << compute_interest() << endl;

emoji Functions check-in 2/2

Predict the output of the following code:

void fun(int x);

int main() {
    int y = 0;
    fun(y);
    cout << y << endl;
}

void fun(int x) {
    x = x + 10;
}

Returning multiple things

Pass by value

So far, all of our functions have used pass by value

Pass by reference

Rules and conventions for pass by reference

Style note: functions with reference parameters should usually be void or return bool (more on that later)

Example 1

Write a prototype for a function that will “move” a point in a 2D plane according to an angle and a distance. Assume the point is represented by two double parameters x and y.

center h:300

Example 2

Implement a function with the prototype void swap(int& a, int& b) that exchanges the values of two ints

Try this on paper for a few minutes, then we’ll go through a solution

Testing: drivers and stubs

Either way you need to “fake” the parts you haven’t written yet

emoji Testing review 1/2

A function with an int parameter num implements the following logic. How many test values are needed to exhaustively test it?

  1. 1
  2. 2
  3. 3
  4. 4
  5. Impossible to test exhaustively
result = num
if num < 0
    result = -num

return result

emoji Testing review 2/2

It is acceptable to hard-code magic numbers for test purposes.

  1. True
  2. False

Test Drivers

You can also use assert or a test framework like GoogleTest , but those are beyond the scope of this course

Function Stubs

What value should the stub return? Something that makes sense in the context of how the function will be used.

Side Tangent: input redirection

This is a bash thing, not a C++ thing - you could do the same with Python

Boolean preview

bool is a data type that can only have two values: true or false

PythonC++Description
====Equal to
!=!=Not equal to
<<Less than
<=<=Less than or equal to
>>Greater than
>=>=Greater than or equal to

Functions can return bool, just like in Python:

def is_even(num: int) -> bool:
    return num % 2 == 0
bool is_even(int num) {
    return num % 2 == 0;
}

Compound Boolean expressions

PythonC++Description
and&&Logical and
or||Logical or
not!Logical not

if statement syntax

if (boolean_expression) {
    // code to execute if true
} else {
    // code to execute if false
}

Coming up next

Textbook Sections 2.4, 3.1-3.2

Extra: another pass-by-reference example

Write a function called sort2 that takes two int parameters and sorts them in ascending order - that is, after a call to sort2(m, n), the smaller value is in m and the larger value is in n.

Hint: you can use the swap function from earlier



Previous: Lecture 03: Functions
Next: Lecture 05: Booleans and Decisions