Please send questions to
st10@humboldt.edu .
* another side-effect -- printing something to the
screen;
* C++ provides MANY libraries of useful classes,
objects, and functions;
* ONE of these is its standard "stream"
input/output library,
iostream
* You include a C++ STANDARD library with these
lines:
#include <iostream> // put the name of the library
// in angle brackets
using namespace std;
* (if you look at your .cpp files, you'll
see that funct_play2 has been putting this
in for you -- so you CAN use what we
are about to discuss in your functions
built with funct_play2;)
* iostream provides two useful objects for
stream input/output:
cout --> used for screen output
(actually standard output)
cin --> used for keyboard input
(I think we'll discuss this next week)
* today, we'll use cout
(typically pronounced "see-out")
* cout has the side-effect of printing the
value of one or expressions to the screen;
(to standard output, which by default is the
screen...)
THIS SIDE-EFFECT IS NOT THE SAME as using
PARAMETERS and return --
it is just a side-effect!
...it doesn't cause any value to be returned
to the caller;
you need a return statement for that;
* MAKE SURE THIS IS CLEAR:
...a return statement ENDS the function --
the return's value is returned as the
function call's value --
the function is exited, and the return's
value is returned to whatever made the
function call;
...a cout does NOT end a function -- it
has its side-effect of printing to
the screen, and then the next statement
in the function is done;
* for example:
cout << 1 << 2 << 3 << 4;
...causes this to be printed to the screen:
1234
cout << 1;
cout << 2;
cout << 3;
cout << 4;
...also causes this to be printed to the screen:
1234
cout << 1 << "really really reaLLY REALLY REALLT "
<< " LONG string";
...causes this to be printed to the screen:
1really really reaLLY REALLY REALLT LONG string
* iostream provides many goodies related to stream
i/o (input/output)
* another object: endl
...put this in an output stream (such as cout)
when you'd like a newline character...
cout << 1 << endl << 2 << endl << 3 << endl << 4 << endl;
...causes this to be printed to the screen:
1
2
3
4
* (you can throw "\n" or '\n' into the output stream
to get a newline, too...)
* boolalpha is another iostream object --
it asks the output stream to format anything of
type bool in alpha format -- as true, false
cout << boolalpha << (1 < 3) << endl;
...causes this to be printed to the screen:
true
...(which is why funct_play2 puts a boolalpha in the
cout stream in the _ck_expect.cpp code it builds...)
* Another example:
int next_wt = 50;
cout << "hi" << 3+5 << 2 * next_wt << endl << 'o'
<< "xx" << endl;
...causes this to be printed to the screen:
hi8100
oxx
* note: cout has default formats for different types;
notice how string's double quotes are not displayed,
nor are char's single quotes?
* boolalpha changes the cout default formatting
for bools from 0, 1 to false, true
* double's default formatting is only for
about 7 significant digits -- it can be
changed using other iostream tools
(we *might* get to more on than later...)
...silly little looping-plus-cout example:
cheer
signature: cheer: int -> int
purpose: expects the number of HIP's desired in
a cheer, and has the side-effect of
printing to the screen that many HIP's
each on its own line followed by a HOORAY!,
and then it produces how many HIP's it printed
int cheer(int num_hips)
examples:
cheer(3) == 3
but also has the side-effect of printing to the screen
HIP
HIP
HIP
HOORAY!
int cheer(int num_hips)
{
int count = 0;
while (count < num_hips)
{
cout << "HIP" << endl;
count++;
}
cout << "HOORAY!" << endl;
return count;
}
* how about writing a function that expects
an array parameter?
* initializing an array at declaration-time:
const int NUM_GRADES = 5;
int quiz_grades[NUM_GRADES] = {95, 98, 87, 70, 99};
* C++ arrays don't have their size "included"
along with them;
* SO: style point: any functions we write that
expect an array parameter will also expect
a parameter giving the array's size (the
array's number of elements)
* to write the signature comment,
put an array's type by putting the type name,
then empty square brackets
// signature: sum_array: double[] int -> double
// purpose: expects an array of numbers and its size,
// and produces the sum of those elements
* how do you declare an array parameter?
...like you declare an array variable, but
without specifying a size (the argument
will have a size...!)
double sum_array(double values[], int num_values)
examples:
const int NUM_VALS = 4;
* to pass an array as an argument --
you can simply give the array's name!
double my_stuff[NUM_VALS] = {1, 100, 10, 2};
sum_array(my_stuff, NUM_VALS) == 113