Recall & Review
beginner
What is function overloading in C++?
Function overloading allows multiple functions to have the same name but different parameters (type or number). The correct function is chosen based on the arguments used when calling it.
Click to reveal answer
beginner
How does the compiler decide which overloaded function to call?
The compiler looks at the number and types of arguments in the function call and matches them to the function signature that best fits those arguments.
Click to reveal answer
beginner
Can two overloaded functions differ only by return type?
No. Overloaded functions must differ in their parameter list. Return type alone is not enough for overloading.
Click to reveal answer
beginner
Example: What will be the output of this code?<br>
void print(int x) { std::cout << "Int: " << x << std::endl; }
void print(double x) { std::cout << "Double: " << x << std::endl; }
print(5);
print(3.14);Output:<br>Int: 5<br>Double: 3.14<br>The compiler calls the correct print function based on the argument type.
Click to reveal answer
beginner
Why is function overloading useful?
It lets you use the same function name for similar actions with different data types or numbers of inputs, making code easier to read and maintain.
Click to reveal answer
Which of these is a valid function overloading example?
✗ Incorrect
Functions must differ in parameter types or number. Option C differs by parameter type. Option B differs only by return type, which is not allowed.
What happens if you call an overloaded function with arguments that match multiple versions equally well?
✗ Incorrect
If the compiler cannot decide which function to call because of ambiguity, it throws a compilation error.
Can default arguments affect function overloading?
✗ Incorrect
Default arguments can cause ambiguity if multiple overloaded functions can match the call with default values.
Which of these is NOT a valid way to overload a function?
✗ Incorrect
Changing only the return type does not create a new overload.
What is the main benefit of function overloading?
✗ Incorrect
Function overloading improves code readability by using the same name for similar operations with different inputs.
Explain function overloading and how the compiler chooses which function to call.
Think about how you pick the right tool for a specific job based on what you need.
You got /3 concepts.
Describe why function overloading cannot be done by changing only the return type.
Imagine two identical keys with different colors; color alone doesn't open different doors.
You got /3 concepts.