=====
CS 111 - Week 9 Lecture 2 - 2025-10-23
=====
=====
TODAY WE WILL:
=====
* announcements
* important additional info about return statements
* CS 111 class style related to blocks and function bodies
* debugging note: ideas to try if one of your function tests
does not pass
* using the design recipe to design some more functions
* prep for next class
=====
* should be working on Homework 7!
=====
a little more about return statements
=====
* return is special in that, when a return statement is
reached, its expression becomes the value of
that function call, and the function ends at that point!
* what if a C++ function HAS no return statement?
* if the function is not INTENDED to return anything --
if it has the special return type of void --
that's fine, the function will end when it reaches
the end of the function body (its closing } )
* (if the function IS intended to return something,
I believe you'll get an error -- if a C++ function is
to return something, it SHOULD have a return statement...)
=====
definition of a C++ block
=====
* this is a C++ block:
{
statement;
...
statement;
}
* the C++ compiler considers that to be a single statement!
and it can be put anywhere a single statement is allowed
to go
* notice, then, that in defining a function, you have a
function header, and the function body is a block!
* and CS 111 class style regarding blocks is as follows:
1. Put { and } each on their OWN line (as shown above)
2. They should be lined up with the preceding statement
3. INDENT the statements within { and } by at least 3 spaces
4. For statements that are intended to be sequential within
a block, line up the beginning of these statements
5. (But for a long statement, indent its continuation(s)
on subsequent lines... like we did for long compound
expressions in Racket)
AND a CS 111 class coding standard for comments:
* put a blank line before and after each comment
=====
some ideas for DEBUGGING...
=====
* compile failed?
* look at the FIRST error message,
NOTE that it includes the line number in the source code
where the compiler got confused!
* (it may have gotten confused AFTER the actual error --
start at that line and work upward!)
* compile succeeded, and got some falses?
* it can be useful to add some cout statement printing the
function call's result and the expected result
* (especially for double values, remember that cout by
default only displays about the first 6-7 significant
digits -- so if the displayed digits are the same,
you likely need to change your test to the style
of seeing if the absolute value of the difference
is small enough --
feel free to copy over demo.cpp's and 111hw7.cpp's
check_within function if you would like)
=====
fun fact about C++ string class
=====
* the string class (the string type) has defined +
for use in appending string instances!!!
const string FIRST = "Charlie";
const string LAST = "Brown";
cout << (FIRST + LAST) << endl; // CharlieBrown
BUT!!!!! this only works if at least ONE of + operands
is a string instance!
the other CAN be a char* or even (gasp!) a char...!
cout << (FIRST + " " + LAST) << endl; // Charlie Brown
* (note: first computes FIRST + " ", a string and a char*,
resulting in a string -- then computes the result
of appending THAT string to the string LAST)
cout << (FIRST + '-' + LAST) << endl; // Charlie-Brown
* (note: first computes FIRST + '-', a string and a char,
resulting in a string -- then computes the result
of appending THAT string to the string LAST)
BUT this FAILS:
cout << ("m" + "oo") << endl; // fails, both are char*
* after class, added these to 111lect09-2.cpp, to the end
of its main function, so you can see them in action