I'm going through this tutorial: https://www.learncpp.com/cpp-tutorial/how-to-design-your-first-programs/ I noticed the author didn't use a parameter in this function:
int getUserInput()
{
std::cout << "Enter an integer ";
int input{};
std::cin >> input;
return input;
}
Would it be okay to do something like this?
int getUserInput(int input)
{
std::cout << "Enter an integer ";
std::cin >> input;
return input;
}
CodePudding user response:
It would work, but it wouldn't make much sense.
The first version of your function is used something like this:
int some_number = getUserInput();
That makes sense; the caller isn't providing any input to the function, so it takes no parameters.
The second version takes a parameter though, so the caller has to provide it. The function doesn't actually do anything with that value though. All of the following behave exactly the same:
int some_number1 = getUserInput(0);
int some_number2 = getUserInput(123456);
int some_number3 = getUserInput(some_number2);
It makes no sense for the caller to provide a parameter to the function since the function doesn't use it at all.