More Functions
5.4: Void Functions and Program Design
Primary uses of void functions:
- Calculating and outputting a result within the function body (rect6.cpp)
- Printing the final output for a program
- Useful output, e.g. instructions or a menu of choices
- Pretty printing: date1.cpp (has a little trick to make sure 3 is printed as 03)
5.5 Functions calling other functions
- It's legal
- date2.cpp chips off the digit issue off its own function
5.6 Using Function Stubs in Program Development
- Often you use a function to defer making decisions about how you are
going to do something (e.g. should I use a weighted average?, how do I want to print it out? ...
- A way to test the syntax of the program before all functions are filled in.
- Stub: the function heading with an empty body.
- Fill in loan0.cpp.
With stubs: loan1.cpp,
With functions: loan2.cpp.
- It would be nice to bury program inputs in a function: loan3.cpp).
- Alas, this does not work (yet).
5.7 Reference Parameters
So far, function calls cannot change the value of a parameter.
Distinction between
- value parameter: the function cannot change the parameter value (loan3.cpp).
- reference parameter: it can (loan3r.cpp).
More later.
5.8 Saving and Reusing Functions
We have talked about libraries that get included using a compiler directive #include.
Can we do this ourselves? Yup: loan5.cpp, loan.h