* most of our parameters this semester
have been scalar (single-valued) pass-by-value
parameters;
the value of the argument is COPIED into
the parameter's location in memory
(and if you change the parameter,
you typically JUST change that local copy,
and it can't affect the original argument)
* C++ also supports pass by REFERENCE --
a pass-by-reference argument has its
MEMORY LOCATION passed to the function it is
calling,
and under the hood it is set up so when
you change the parameter, you actually change
the calling argument
you indicate that a parameter is pass-by-reference
in C++ with:
desired_type& desired_param_name
* I could use this to write a little swap function:
(we'll put & after the type of a pass-by-value parameter
in our signature, to WARN the user we may be mucking
with their argument!!)
signature: string& string& -> void
purpose: expects two string variables oor
things I CAN CHANGE, and returns nothing,
BUT has the SIDE EFFECT of making the 1st
argument's value the original value of the
2nd argument, and vice versa
examples:
string last_name = "Bob";
string first_name = "Bobby";
then if I run:
swap(last_name, first_name);
...then afterwards,
last_name == "Bobby"
first_name == "Bob"
and what if I now had:
string mid_init = "B";
swap(mid_init, first_name);
...then afterwards:
mid_init == "Bob"
first_name == "B"
*****/
void swap(string& str1, string& str2)
{
string temp;
// SAVE a copy of str1's value
temp = str1;
// now it is safe to overwrite str1
str1 = str2;
// now it is safe to overwrite str2
str2 = temp;
}