CS 111 - Week 11 Lecture 2 - 2016-11-03
==========
tomorrow's lab quiz:
* read and write cout statements
* maybe a simple main-related question
* know its function header
* know it should return EXIT_SUCCESS
* know its #includes
* it needs a #include of the .h for EACH function
it calls
* and EVERY C++ program must have a main function
* and after you compile/link/load, when you run it,
the action starts at the main function
==========
* (and we used FileZilla as a Mac stand-in for WinSCP,
and showed copying over say_sound.h, say_sound.cpp, and
say_sound_test.cpp from Tuesday from my laptop to nrs-labs;
* and then we used ssh as a Mac stand-in for PuTTY,
and showed logging into nrs-labs and
compiling, linking, and loading the program consisting
of the functions say_sound and the main function in
say_sound_test.cpp using:
g++ say_sound.cpp say_sound_test.cpp -o say_sound_test
* ...and we then ran the compiled resulting test program using:
say_sound_test
* we also demo'd using the optional tool
compile-helper
...which asks you pointed questions to be able to build
the g++ command above, if you think you might find that
helpful;
==========
Intro to MUTATION
(and local variables, and assignment statements!)
* CONSIDER -- in both Racket and C++, we have been giving
names to values for a while!
(define NUM-WIDGETS 300)
const int NUM_WIDGETS = 300;
(define (tank-volume length width height)
(* length width height)
)
double tank_volume(double length, double width,
double height)
{
return length * width * height;
}
(tank-volume 3 5 8)
tank_volume(3, 5, 8)
...parameter length is set to 3
parameter width is set to 5
parameter height is set to 8
(tank-volume 2 4 6)
tank_volume(2, 4, 6)
...parameter length is set to 2
parameter width is set to 4
parameter height is set to 6
* we've been programming in a rather
"functional" style so far --
we don't change values in names once we have
set them,
our functions tend to expect parameters and
return a value based on those;
* in Racket and C++,
you can also program in more "IMPERATIVE" style
(sometimes called PROCEDURAL style)
...and we're delving into this style there
* in this style, we make deliberate use
of MUTATION - deliberately changing the
value in a name - to useful effect
* WHAT name? WHAT kind of identifier
will we change/will we mutate?
* NOT named constants!!!
* not function names, either (typically)
* not MOST kinds of parameters
(but more on that later in the semester)
* USUALLY, we do this with so-called
LOCAL variables
* WHAT is a LOCAL variable?
* it is declared, in its own statement,
INSIDE of a BLOCK
(a block is a set of { } )
{
int amount;
}
* scope: the portion of your code in which
a name has a value
* the SCOPE of a parameter is its function's
function body
* the SCOPE of a local variable is from
where it is declared to the end of
that block (its closing } )
* here's the thing: you often make a
local variable so that you can change it;
these are usually what are changed in
imperative programming
* USEFUL MENTAL MODEL:
think of a local variable as a named "box"
in the computer memory, waiting for a value;
once set, I can USE that value,
AND I can CHANGE that value
(we drew boxes on the white board for each local
variable, and even for each parameter, for the
rest of the examples below...)
* how can you PUT a value into this "box"?
* for a parameter, you do so by simply
calling its function --
the corresponding argument's expression's value
becomes the value of that parameter
* we'll see (soon, maybe tomorrow??) that you
can also give a value to a local variable
using INTERACTIVE INPUT (cin)
* another way that is QUITE typical for a
*local* variable is with an assignment
statement:
my_var = expr;
the value of expr is ASSIGNED to my_var
(you NEEEEEED to have declared my_var BEFORE this!!!!!)
my_var's value is now the value of expr
{
int amount;
// this sets amount to 8
amount = 5 + 3;
cout << amount << endl;
amount = 42;
cout << amount << endl;
}
...the above (if in the proper context) would output WHAT
to the screen
8
42
* (you CAN declare AND initially set a local
variable in ONE statement --
string vacation_spot = "Arcata";
int quant = 0;
double stuff = 12.3;
)
* IMPORTANT C++ STYLE RULE
and ALSO a CS 111 STYLE RULE:
* ALWAYS INITIALIZE a LOCAL VARIABLE BEFORE YOU
USE IT.
BAAAAAAAAAAD:
=============
int num;
cout << num << endl;
...what is in num, declared but yet set?
...that is NOT specified -- defined -- in the C++
language definition,
so different compilers do different things,
INCLUDING leaving whatever garbage was there before;
...give it an initial value -- INITIALIZE it --
and THEN use it!
GOOOOOOOOOD!
============
int num = 100;
cout << num << endl;
int another_num;
another_num = 200;
num = another_num + 1;
* CONSIDER:
int number = 3;
int quantity = 7;
number = quantity + 1;
// number is now 8
* SEMANTICS of the ASSIGNMENT STATEMENT:
local_var = expr;
...local_var is sometimes called the LEFT HAND SIDE
or LHS of this assignment statement
...expr is sometimes called the RIGHT HAND SIDE
or RHS of this assignment statement
FIRST the expression on the RHS is evaluated/determined,
THEN the resulting value is assigned to the LHS
BAAAAAAAAAAAAD:
===============
3 + 5 = 8;
5 = 6;
sqrt(4) = 2;
...you cannot assign a value to
3 + 5
5
sqrt(4)
* int number;
number = 100;
number = 1 + 3;
// now number is 4 ...