Today we practiced using functions.
We wrote computeArea.cpp,
which modified the computePi.cpp code from discussion 2-9
to use functions instead.
A lot of what we discussed isn't in the code this time, since I answered a bunch of very good questions out loud.
Here are a couple of highlights at least:
The main benefits of functions are:
They let us organize our code nicely, so that each step is coded in a separate place.
This often makes main() easier to read.
It also makes code easier to debug, since we can write tests for each function separately.
Functions let us avoid writing the same code twice.
If we need to do the same task at multiple points in our program,
we can just put that code in a function and call it whenever needed.
Avoiding code repetition gives us less chances to make typos/bugs,
and also makes life easier if we want to change how the program works later.
It's legal to define our functions at the top of the .cpp file.
Alternatively, we can put the function prototypes at the top and the actual definitions at the bottom.
For short, simple functions this difference isn't a very big deal,
but for longer functions it's generally better style to put the definition below.
If you put definitions at the top, they need to be in the right order
so that your function calls refer to functions that've already been defined further up.
If you put definitions at the bottom, you don't need to worry about this.