Please send questions to st10@humboldt.edu .

CIS 130 - Week 9, Monday, 3-22-10

MUTATION...

*   so far, we've been happy to set up names, give them
    values, and use them;

Scheme:
(define WIDTH 300)      

C++:
const int WIDTH = 300;

double rect_area(double length, double width)
{
    return length * width;
}

rect_area(100, 2)  <-- length is set to 100, width is set to 2
rect_area(5, 4)    <-- length is set to 5, width is set to 4

*   we've been programming in a FUNCTIONAL style thus far --
    we set the value for a name, then use it, but we don't really
       change it once we've set it;

    we call functions that are mostly "pure" in the sense that
       they expect certain values, and they then produce, or return,
       some result BASED on the expected values;

*   in both Scheme and C++, there is another style, known as
    imperative style, where you deliberately change the value
    assigned to some name, to achieve a desired effect;

*   mutation, then, is CHANGING the value assigned to a name;

int value;   <-- if you write a declaration like this
                 INSIDE the body of a function, that's called
		 a LOCAL VARIABLE -- it only has meaning,
		 or SCOPE, inside that function.

                 Can think of a little integer-sized block
                 of memory, with the name value
      -----
value |   |
      -----

*   how can we give a value to this local variable?

    *   there are SEVERAL ways -- but let's START with the basic
        ASSIGNMENT STATEMENT:

	value = 3;   
              ^ this = is an ASSIGNMENT OPERATOR (NOT comparison for equality!)
        meaning: make value's value be 3
        (assign 3 to be value's value)

        any_local_variable = expr_of_appropriate_type;
  
        ...the VALUE of the expression on the right of the =
           is ASSIGNED to the variable on the left of the =

    *   with mutation, I could just keep changing a variable's value!

        int quantity;
        quantity = 3; /* now quantity has the value 3 */

quantity -----
         | 3 |
         -----

        quantity = 26; /* now that 3 is GONE, and quantity has the value 26 */

quantity -----
         |26 |
         -----

        that's mutation! I changed the value of the name quantity --
	now its value is 26.

*   so: important point: at the time a statement gets executed,
    you are using the value of all the names involved AT THAT POINT;

consider the following weird little function:

int playing(int amount)
{
   int look_at_me; /* this is a local variable in function playing */

    look_at_me = 6;

    look_at_me = 47;

    return amount + look_at_me;
}

playing(5) == 52

*   to test yourself further: consider the following fragment
    (fragment: several lines of code ASSUMED to be part of a bigger whole)

    int i;
    
    i = 7;
    i = i + 1;
 
    What is i after the 3rd statement above? It is 8 --

    i = i + 1; means,
        assign to i the value of the expression that adds the current
	            value of i plus 1;
        So, assign to i the value of 7 + 1, or 8;
	NOW, after this statement, i is 8

That is, for an assignment statement,
    you EVALUATE the expression on the right-hand-side of the =,
    and that value becomes the value of the variable (or thing-that-can-be-set)
       on the left-hand-side of the =

    *   we often just say RHS to mean the expression on the right-hand-side
        of the = in an assignment statement,
	we often just say LHS to mean the thing-that-can-be-set on
	the left-hand-side of the = in an assignment statement

Consider this fragment:

int amount;
amount = 10;
amount = amount * 3;

...what is the value of amount after the 2nd line? 10
...what is the value of amount after the 3rd line? 30 (10 * 3)

*   some frequent C++ shortcuts:
    *   we often find ourselves modifying a current value of something;

        count = count + 1;
        cost = cost * 1.08;
        remainder = remainder / 3.0;
        countdown = countdown - 1;

        C++ provides the operators += *= /= -=

	count = count + 1; 
	/* is the SAME as */
        count += 1;

        cost = cost * 1.08;
        /* is the SAME as */
        cost *= 1.08;

        remainder = remainder / 3.0;
        /* is the SAME as */
        remainder /= 3.0;

        countdown = countdown - 1;
        /* is the SAME as */
	countdown -= 1;

Let's say you have this fragment:
int value = 3;   /* you can declare and initially assign in one step */
int quantity = 20;
quantity += value;

*   common C++ shortcut #2 -
    ...we end up adding 1 or subtracting 1 from something a LOT.

    C++ gives a shorthand for that, too: ++

    When you put ++ BEFORE or AFTER a variable name (or a thing-that-can-be-set)
    ...the result is to ADD ONE to the value in that variable

    int val = 7;
    val++;
    /* val is now 8 */

    val++; /* has the SAME effect (afterwards) as
    val += 1; /* has the SAME effect as
    val = val + 1;

    (get the joke? C++ is supposed to be 1 "better" than C...)

    There's also --, to subtract 1

    IF its by itself, then the END result is the same whether you put ++
    before or after the variable name:

    quant++;
    ++quant; 

    ...both add 1 to quant;

*   IF you use an expression with ++ IN the RHS of an assignment statement,
    then there IS a subtle but IMPORTANT semantic difference between
    putting ++ before or after a variable:

    value = 3 + (quantity++);
    value = 3 + (++quantity);
 
    IN an expression, ++quantity will INCREASE quantity by 1
        (people often say, will INCREMENT quantity),
	and THEN use the new value of quantity in the expression;

    IN an expression, quantity++ will USE the current value
        of quantity in the expression, and THEN increment it
	(and then increase it by 1)

int quantity;
int value;
quantity = 5;
value = 3 + (quantity++);
/* at THIS point, quantity is 6 and value is 8,
   BECAUSE quantity's current value, 5, is added to 3
   BEFORE quantity is increased to 6 */

int quantity;
int value;
quantity = 5;
value = 3 + (++quantity);
/* at THIS point, quantity is 6 and value is 9.
   BECAUSE quantity is increased to 6, and THEN used
   to add to 3 to get value's new value of 9 */

*  You don't HAVE to write expressions with ++ on the RHS --
   but you should recognize what it MEANS when you do;

Wednesday: we'll start interactive input and output
	   (and maybe more...)