Quiz 18: A step back to review reference variables

  • Due Mar 28, 2016 at 10:10am
  • Points 6
  • Questions 6
  • Time Limit None
  • Allowed Attempts 2

Instructions

The setup for this quiz is long, since we've been away for a week and we didn't talk much about references in class the first time around. Please read the below. There are some important notes at the bottom. Then consult 5.9 and run the code snippets in the quiz to see what's happening.


Back in the stone age six weeks ago, you only knew how to hard-code values in your programs, like this:

int main()
{
    cout << "Hello, world!" << endl; // these strings are "hard-coded"
    cout << "Goodbye, world!" << endl; // changing them = rewriting program
    return 0;
}

You quickly learned how to use variables to stand in for these literal values, but still with hard-coding:

int main()
{
    string greeting = "Hello, world!"; // still hard-coded, assigned when
    string farewell = "Goodbye, world!"; // we compile the program

    cout << "Hello, world!" << endl; 
    cout << "Goodbye, world!" << endl;
    return 0;
}

You were pleased to be able to write interactive programs that do not respond the same way every time, instead getting input from the user. This is really different from hard-coding values:

int main()
{
    string firstname;
    cout << "Hi there! What's your name? (enter name): "; // a polite prompt 
    cin >> firstname; // this program obtains some values interactively
    string farewell = firstname + ", I have to go now. My home planet needs me!";
    cout << farewell << endl;
}

While some people wish only to write programs that get input from the user forever, that is only the first step we have taken. A really flexible and reusable program uses a function to receive its input:

string get_name()
{
    string name;
    cout << "Hi there! What's your name? (enter name): "; // Right now, we
    cin >> name; // are getting input from keyboard, but later we might
    return name; // want to read it from somewhere else, like a file
}

int main()
{
    string firstname = get_name(); // assign the return value into a variable
    string farewell = firstname + ", I have to go now. My home planet needs me!";
    cout << farewell << endl;
}

With this last program, if we want to change the mechanism by which the name is read into memory, we don't have to rewrite the whole program. The main() function doesn't care how the get_name() function works. The only thing that matters is that it return a string. The way main() is coded, it is expecting the right-hand side of the first assignment statement to evaluate to something of type string, which only happens if get_name() returns such a value.

You now have all the techniques to develop real programs in C++ except one. To move forward, we need to have a fuller understanding of reference. Since we are all aware that global variables are the devil, we need an alternative way to make variables accessible to different functions at different points during program execution. Reference is often a convenient way to do this, especially in the following situations:

  • The function needs to read from or write to an array. (Arrays can ONLY be passed by reference in C++.)
  • The function needs to assign, modify, or update several values at once. If a function is only changing one variable, it's usually possible (though not always desirable) to avoid passing by reference. If you are writing a function that changes several variables, it should almost certainly accept those variables as reference parameters and modify them "in place" or "by side effect". This is as opposed to the more elementary way, by assigning the return value of a function into the variable that needs updating.

One word of caution as we start the second half of the semester. C++ is a mature language with many millions of lines of existing code, a huge global following, and an international committee that makes decisions about how it evolves. It is descended from the even more mature language C, and many of the most experienced programmers learned C++ after they were already good at C. I am not trying to teach you how to program in a C style, but rather a C++ style. If you look for help online, you are very, very likely to get answers that are more appropriate for C than for C++ (because there are so many people who learned to code that way: it's not wrong, it's just different). The code will probably still work, but it will be harder to understand, harder to debug, harder to maintain, and probably even harder to type in. It also won't have much to do with how we talk about things in class: it will just be a magic spell to you. Rely first on the book, me, Sam, and your classmates for help.

Reread 5.9 carefully and think about the examples and self-check questions before taking the quiz. Make sure to read the Programming Tip and Special Topic at the end of the section.

Only registered, enrolled users can take graded quizzes