True False C++ for string arguments?
T/F: By passing a string argument to a function, we can make functions more re-usable.
T/F: ^ This is true for functions that control input - prompts are ideal for string arguments.
T/F: As always we pass strings by reference to avoid copying issues of value arguments.
T/F: But, since we aren't going to change the prompts, we make the arguments a constant reference.
T/F: Being constant references instead of plain, we can provide default values for these arguments.
- husoskiLv 72 years agoFavorite Answer
These are generalizations, of course, so generally speaking...
1. True: It's almost automatic. A function with parameters usually produces different results when called with different argument values. So, it has more ways to potentially be useful.
2., True, but this is really just an example supporting the previous statement.
3. True, Avoiding value copying is one of the reasons to use a reference parameter in a function. The other is to allow a function to modify the caller's object. The next statement says more about the first part.
4. True, but it's not stated strongly enough. A C++ function should *always* declare a reference parameter as a const reference unless the function is intended to change or store values using that argument. Get in the habit of making all reference parameters const, and only take the const off when you have a good reason to so.
5. Mostly true. You can have a default value for a non-const reference, but the value must name an object. Literal constants like "prompt" or 42 won't work. Most cases where the syntax works aren't useful in a practical program.
- Anonymous2 years ago
Questions 1 and 3 and answered by questions 2 and 4.