CS 106B Lecture 2: C++ Functions: Wednesday, September 28, 2016
CS 106B Lecture 2: C++ Functions: Wednesday, September 28, 2016
Programming Abstractions
function
Fall 2016
Stanford University
result
Computer Science Department
reading:
Today's Topics
• Logistics:
• Signing up for section
• CS 106L
• Qt Creator installation help on Thursday
• Stanford Local Programming Contest: Saturday
• Homework 1: Fauxtoshop!
• Due Friday, October 7, at Noon
• A note on the honor code and cheating
• YEAH Hours tonight! (will be recorded!)
• Functions
• Some review — functions are very similar to Java functions!
• Value semantics
• Reference semantics
• Reading Assignment: Chapters 2 and 3
Logistics
•Signing up for section: you must put your available times by Sunday
October 2 at 5pm (opens Thursday at 5pm).
•Go to cs198.stanford.edu to sign up.
•CS 106L: A great opportunity to dig a bit deeper into "real" C++, and to see
interesting programming examples. Meets Tu/Th 1:30-2:50 in Hewlett 101.
Scatter!
Assignment 1: Fauxtoshop!
Edge Detection!
Assignment 1: Fauxtoshop!
Compare Images!
Fauxtoshop
•The program you write will utilize:
•Functions
•Constants
•Loops
•I/O (cout, getLine(), getInteger())
•Reference semantics, Value semantics
•Strings
•Logic
•Nicholas Cage
•We will discuss all of the above before the project is due
•Get started early! (Idea: finish Scatter by Friday!)
•Go to YEAH hours! Go to the LaIR!
•This is a non-pair programming program — you must work independently
•Due: 12pm (Noon) on Friday, October 7
Fauxtoshop
A C++ function can only return a single type, and if you want to return
multiple "things," you have to do it differently (unlike in languages
such as Python). We will cover this later, as well.
Function Example, brought to you by
#include <iostream> (bus drivers hate them!)
#include "console.h"
int main() {
for (int i=99; i > 0; i--) {
bottles(i);
}
return 0;
}
Function Example: Output
99 bottles of Coke on the wall.
99 bottles of Coke.
Take one down, pass it around, 98 bottles of Coke on the wall.
...
int main() {
for (int i=99; i > 0; i--) {
bottles(i);
}
return 0;
}
Function Example 2
// Function example #2: returning values
#include <iostream>
#include "console.h"
#include <iostream>
#include "console.h" Output:
using namespace std;
•When you run the program in Debug mode (the green triangle with the bug on it),
the program will stop at that point. Let's see this in action!
Function Example 2: Debugging
•Notes from live debugging:
•You can see variable values as the program executes
•You use the following buttons to continue the program:
Go to next
Go to next
stop line and
line but not
running into Finish the
continue into functions
functions function
to next
and leave
breakpoint
it
•Debugging effectively takes a little time to learn, but is super effective if you
have hard to find bugs.
Declaration Order
#include <iostream> •Believe it or not, this program does not compile!
#include "console.h"
•In C++, functions must be declared somewhere
using namespace std; before they are used.
const string DRINK_TYPE = "Coke"; •But, we like to put our main() function first, because
int main() {
it is better style.
for (int i=99; i > 0; i--) {
bottles(i);
}
return 0;
}
•Without references, you can't write a swap function to swap two integers. This is
true about Java. What happens with the following function?
/*
* Attempts to place a's value into b and vice versa.
*/
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
}
•Answer: the original variables are unchanged, because they are passed as copies
(values)!
Reference Example
•With references, you can write a swap function to swap two integers, because you
can access the original variables:
/*
* Places a's value into b and vice versa.
*/
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
•Answer: the original variables are changed, because they are passed as
references !
Tricky Reference Mystery Example
What is the output of this code? Talk to your neighbor!
•Advanced Reading:
•Wikipedia article on C++ References: https://en.wikipedia.org/wiki/
Reference_(C%2B%2B)
•More information on C++ references: http://www.learncpp.com/cpp-tutorial/611-
references/
•C++ Newton's Method question on StackOverflow: http://
codereview.stackexchange.com/questions/43456/square-root-approximation-with-
newtons-method
•If you are super-brave, look at the square root C++ function in the C library: http://
osxr.org:8080/glibc/source/sysdeps/ieee754/dbl-64/e_sqrt.c?v=glibc-2.14#0048