Hi, is there a way to execute a line of code if the function is provided with 2 parameters and another line of code if it's provided with only 1 instead? Let's say I have this function:
1 2 3 4 5 6 7 8 9 10 11 12
int blah(constchar* prompt, int y = 0)
{
std::cout << prompt << std::endl;
if (y is provided)
{
do something with y;
}
if (y is not provided)
{
do another thing in general;
}
}
If in main() I call blah("The prompt", 5);, I want it to execute the if (y is provided) code, if I call blah("The prompt"); I want it to execute the if (y is not provided) code. What should be next to if? Thanks!
This is what function overloading is for. Create two versions of the function: one with two required parameters and one with one required parameter. Default parameters are only useful when your code is the same in both cases.