CS 112 - Week 6 Lecture 1 - 2022-09-27

TODAY WE WILL:
*   announcements
*   ROAD to POINTERS and DYNAMIC MEMORY MANAGEMENT!
*   STOP 0 - input parameters, output parameters,
    and input/output parameters
*   STOP 1 - reminder of pass-by-value
*   STOP 2 - pass-by-value and arrays
*   STOP 3 - pass-by-reference
*   [if time] STOP 4 - C++ pointer basics
*   prep for next class

*   Should be working on Homework 4!

*   Current reading: Savitch text, CHAPTER 9
    (for pointers and dynamic memory allocation)

=====
STOP 0 - input parameters, output parameters,
         input/output parameters
=====
*   input parameters - arguably what we use
    the most!

    ...they just provide input to a function

*   output parameters - less frequent, but we've
    seen a few

    ...a parameter whose purpose is JUST to be
       changed by a function
       (changing the corresponding argument accordingly)

*   input/output parameters - when you want to
    BOTH use the parameter AND (possibly) change its
    contents (change the corresponding argument's
    contents)

    ...like a function that sorts an array parameter!

=====
STOP 1 - reminder of pass-by-value
=====
*   there are MULTIPLE ways a language MIGHT
    be designed to pass arguments to parameters...

    C++'s default for this is: pass-by-value

    for a scalar (single) value,  <-- that is, not an array!
    for pass-by-value,
    a COPY of the argument expression's
        VALUE is copied into the parameter variable
=====
STOP 2 - pass-by-value and arrays
=====
*   under the hood, C++ does NOT pass an array argument
    by COPYING all of its values into an array parameter;

    C++ "sees" an array as the address of its
    first element;

    so a pass-by-value array is passed by
    copying the address of its first element -- where
    it is -- into the array parameter

    *   a C++ pass-by-value array parameter
        cannot change WHERE its argument array
	starts -- BUT!!!

        it CAN go to where it starts (it has a copy
	of its address) and both USE and CHANGE what is
	there.

    *   SO! a C++ parameter can be used as
        an input parameter, an output parameter,
	or as an input/output parameter

=====
STOP 2 - aside - const with array parameters
=====
*   given what is copied for a pass-by-value
    array, how can a programmer ASSURE a user
    that a given function WILL. NOT. CHANGE.
    an argument array?

    C++ says you can precede an array parameter
    declaration in a function/method header
    with const to specify this:

    void print_nums(const double my_nums[], int size)

=====
STOP 3 - pass-by-reference
=====
*   C++ allows you specify that a particular parameter
    use a different argument-passing approach than
    its default of pass-by-value;

    pass-by-reference

    When a parameter is specified to be pass-by-reference,
    when its function is called,
    the value assigned to that parameter
        is the ADDRESS of its argument
	(instead of a copy of argument's value)

    INSIDE the function,
        every time a statement or expression references
        that parameter, the computer goes to the address
        in that parameter to grab (or change) that
        argument;

    So if you change the parameter in the function,
    it DOES change the corresponding argument,
    even for a scalar, single-valued parameter

    AND this gives us a way, possibly with fewer errors
    than the alternatives, to write a function with
    scalar output parameters and scalar input/output
    parameters

*   HERE's how you ask C++ to give you a pass-by-reference
    parameter:

    ret_type funct_name( ... param_type & param_name, ... )

    *   C++ does not care if there are spaces before or
        after that &

        all of these are syntactically correct:

        void swap(int& val1, int& val2)

        void swap(int &val1, int &val2)

        void swap(int & val1, int & val2)

        let's check:
	void swap(int&val1, int&val2)

    ...and now you just use the parameter
       as if it were the type without any &
       (use it, change it, etc.)

    ...and you give it arguments THAT HAVE ADDRESSES
       (the arguments must be lvalues)

    See working example function swap,
    in swap.h, swap.cpp, swap_test.cpp