In my book, all throughout the Function chapter, when they call a function that was previously declared, the contents of the parameters were exactly the same.
#include <iostream>
#include <string>
usingnamespace std;
char askYesNo1();
char askYesNo2(string question);
int main()
{
char answer1 = askYesNo1();
cout << "Thanks for answering: " << answer1 << "\n\n";
char answer2 = askYesNo2("Do you wish to save your game?");
cout << "Thanks for answering: " << answer2 << "\n";
return 0;
}
char askYesNo1()
{
char response1;
do
{
cout << "Please enter 'y' or 'n': ";
cin >> response1;
} while (response1 != 'y' && response1 != 'n');
return response1;
}
char askYesNo2(string question)
{
char response2;
do
{
cout << question << " (y/n): ";
cin >> response2;
} while (response2 != 'y' && response2 != 'n');
return response2;
}
In the beginning of the following chapter, the book demonstrates constant references. I understand the constant references but I don't understand why this code works when the called function's parameters are not the same as the one that was declared. Here's the code:
You seem to have a slight misunderstanding of how parameters work. Basically, when you pass an object into a function, the function does not know (and doesn't care) what the original name was. Instead, you refer to it by the name that the function gives it (e.g. vec for your display function).
I know that it doesn't have to have the original name of the vector in main but when the function was declared, as void display(const vector<string>& inventory); why did it not have to be called the same way?