* we've seen side-effects before,
effects or results from running
a function BESIDES the value
that function returns;
in BSL Racket:
* define - JUST had side-effects
* file output - return value PLUS
side-effects
* big-bang - return value PLUS
side-effects
* we're going to talk about even
more possible side-effects now;
* today, first:
SCREEN OUTPUT
* PRINTING something to the screen
is NOT the same as RETURNING something;
* ...you do these differently in
C++;
how do you return something?
...you use a return statement!
return desired_expr;
^ that desired_expr becomes the
value of that function call,
and you exit the function
and return that value
* you can print to the screen
without ending your function,
as much as you like,
at different places in the function!
* OK, how do you print to the screen,
then, in C++?
...it depends on which C++ input/output
library you are using;
...we'll use a very standard library
named:
iostream
* to use stuff from a standard
library in C++,
you #include that library
putting its name in angle brackets:
#include <iostream>
(to include your OWN code,
you will #include its .h file
putting THAT name in double quotes)
#include "circ_area.h"
* the # is special,
it means that is a precompiler directive,
something to be done BEFORE
the code is compiled/translated
* SO --
#include
literally includes a file's contents
BEFORE translating your code...!
* there is also the concept of
namespaces in C++;
in this class,we are using the standard
namespace, std
...after your last #include,
we will put
using namespace std;
* SO! I have:
#include <iostream>
using namespace std;
double play_around(double val)
{
}
* now, in my function, I can use
an object defined in iostream
called:
cout <-- pronounced see-out
...to print to the screen
(an object is an instance of
a C++ class...)
* cout can use a binary infix operator
<<
cout << expr_to_print
...and this operator,
using with the cout object,
has the side-effect of
writing the value of
expr_to_print to the screen,
following cout's default
formatting
* you can "stack up" << operators
in a single cout statement:
cout << expr1 << expr2 << ... << exprN;
...be sure to put ; after the last
expression to be output,
to END this STATEMENT;
* iostream has MANY objects and functions
and goodies in it;
cout is one of them;
endl is another object from iostream --
you can add it to a cout stream to
output a newline character
* yes, is OK to also put
"\n" or '\n' into a cout stream
to get a newline, also
boolalpha is another from iostream --
when you add it to a cout stream,
any bool value put into the stream
will be displayed as either:
true
false
* CS 111 CLASS STYLE STANDARD:
* we'll use boolalpha when
cout'ing a bool value
* DEFINITION of a C++ program:
* a collection of functions
in which at least one has the name
main
...and when you run that program,
the main function is executed;
* here are two "legal" main headers:
(after class, removed one mentioned in class
because it appears it is not considered as standard
any more...)
int main() <-- we'll use this
int main(int argc, char* argv[])
^ number of command-line arguments
and an array of old-style
strings containing those
command line arguments
(and this is beyond the scope
of CS 111...)