Homework: None



Switch Statements
This is the first lesson where we touch on switch statements. If you take a look at them, they work almost exactly the same as an if statement. The only major difference between them is the absolute necessity to use break. Otherwise you can use the same bracket structure (though it’s not required in the cases inside of a switch, I prefer it for readability), and the same flow of logic.

Here’s an example of an if and a switch doing the same thing.

In general I find it easier to avoid switch statements in C++, but some people do feel as though they are used well for menus and things of that nature. It’s also more readable to most people that case is the result of a choice of a value, rather than an arbitrary test (which is what an if portrays). This is the other advantage of switch statements. They are almost always tied to user choices.


Homework: None



What is StringStream?
StringStream is a library that is used to convert from strings to numeric types in C++. StringStream models it’s usage behaviors after iostream so it’s easy to learn for people that are familiar with the basics of C++. StringStream replaces older methods of doing this like atoi or itoa and gives a much safer, error-free method of handling data parsing in C++.

Homework: Create a function that allows 2 variables to be set to it, multiply them together, output the result, return the outcome to main, add 5 and redisplay the outcome.



What is a function
A function is a section of code that can be called independently from anywhere else in your program to perform a specific task or group of tasks. Functions are able to be passed variables, called “arguments” that are able to be used in the function. These variables are usually passed by “value” (meaning a copy of the variable is made and passed to the function) and in certain cases are passed by reference (meaning the item of the argument itself is passed). Pass by referent will be touched on next lesson, and later on in the series.

Function declarations are broken into 3 parts. The return type, the name, and the argument list.

int addFive (int x). In this example we can see the “int” before addFive. This is the return type. That means that this function is going to put a number where it was called. For example y = x + addFive(x); Would be the same as y = x + (x+5); As long as addFive simply adds 5 to a number and then returns it. The name of this function is addFive, and while function names don’t need to be descriptive, it is important that you assume you won’t be the only person reading your code and that it should be understandable just by reading it. For example if I had the above function named addFive and it added 26, it would be very misleading. The third and final part (int x) in this case is the argument list. What that means is that you are passing in an int as a variable, and when you’re inside the function you can refer to this int as x.

The return type also dictates another piece of code. If you specify anything other than void (return nothing) as a return type, then you need to return a variable / constant of that type.

For example, the following are all valid examples of returning an int.


A few example functions and how you could call them

Homework: Using an array of strings for days of the week and an array for days (your choice on type) in the months of the year, ( 31, 28,…) Figure out the last day of a user input month for the year 2015, or 2016. Ex: 2015, July.



What is passing by reference?
Passing by reference is when you pass an argument into a function directly. The default way which you pass arguments into a function is called “Pass by value” in which a perfect copy of the variable you are passing is made, and the one you passed is not affected by any actions taken within the function. Pass by reference passes the variable itself into the function (via reference, which we will touch on later), and any alterations made to that variable throughout the course of the function are passed made directly to the variable itself and thus persist outside of the function even without assignment.

The only difference in syntax between passing by reference and passing by value is an ampersand (&) which is put before the variable name in the function’s argument list.

int addFive(int &myNumber){} // pass by reference
int addFive(int myNumber){} //pass by value