Charlotte Curtis January 22, 2024
#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
Textbook Sections 4.5, 5.1-5.5
Python only has two scopes: global and local
C++ has block-level scope
{}
if
for
int global_var = 23; // bad idea, but legal int main() { int local_var = 42; // only accessible within main cout << global_var << endl; // 23 cout << local_var << endl; // 42 }
Parameters are local variables that are initialized with the values of the arguments
Do not redefine function parameters!
Common error in COMP 1701:
def some_function(arg_1: int, arg_2: int) -> int: arg_1 = 42 arg_2 = input("Enter a number: ") return arg_1 + arg_2
temp
x
int
double
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);
int interest = compute_interest(1000, 0.05, 3);
compute_interest(1000, 0.05, 0.5);
double interest = compute_interest(1000, 0.05, 3);
cout << compute_interest() << endl;
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; }
void
def get_initial_and_age() -> tuple[str, int]: initial = input("Enter your initial: ") age = int(input("Enter your age: ")) return initial, age
So far, all of our functions have used pass by value
void increase_salary(double salary, double percent_increase) { salary = salary * (1 + percent_increase); }
double wage = 10000.0; increase_salary(wage, 0.05); cout << wage << endl;
Use with caution! Side effects can lead to chaos
&
void increase_salary(double& salary, double percent_increase) { salary = salary * (1 + percent_increase); }
Style note: functions with reference parameters should usually be void or return bool (more on that later)
bool
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.
y
Implement a function with the prototype void swap(int& a, int& b) that exchanges the values of two ints
void swap(int& a, int& b)
Try this on paper for a few minutes, then we'll go through a solution
Either way you need to "fake" the parts you haven't written yet
A function with an int parameter num implements the following logic. How many test values are needed to exhaustively test it?
num
result = num if num < 0 result = -num return result
It is acceptable to hard-code magic numbers for test purposes.
main
cout << "my_func(2) = " << my_func(2) << endl; cout << "my_func(-2) = " << my_func(-2) << endl; cout << "my_func(0) = " << my_func(0) << endl;
You can also use assert or a test framework like GoogleTest, but those are beyond the scope of this course
assert
int my_func(int whole_num, double dec) { return 0; }
What value should the stub return? Something that makes sense in the context of how the function will be used.
$ ./a1 Enter the range of R0 values (0 - 20): 0.5 12 Enter the range of p values (0 - 1): 0.1 0.95
$ ./a1 < input.txt
This is a bash thing, not a C++ thing - you could do the same with Python
bool is a data type that can only have two values: true or false
true
false
==
!=
<
<=
>
>=
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; }
and
&&
or
||
not
!
bool in_range = x > 0 && x < 10
if (boolean_expression) { // code to execute if true } else { // code to execute if false }
()
else
Textbook Sections 2.4, 3.1-3.2
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.
sort2
sort2(m, n)
m
n
Hint: you can use the swap function from earlier
swap
Do a demo showing scope
draw diagram of calling and assigning
Draw the memory location
Demo with expression
How should you approach assignment 1? However you like!
Do a demo with reading values
Reminder: % only works on ints