C++ function overloading: which statement about parameter counts and types is correct?

Difficulty: Easy

Correct Answer: Overloaded functions can accept same number of arguments.

Explanation:


Introduction / Context:
Function overloading lets you offer multiple functions with the same name but different parameter lists. The compiler chooses the best match based on argument types and counts at the call site. Clarifying what may differ (or remain the same) across overloads avoids myths about required differences in both arity and types, or about return types controlling overloading (they do not).


Given Data / Assumptions:

  • Overload resolution uses parameter types, qualifiers, and counts.
  • Return type alone cannot distinguish overloads.
  • We compare claims about what “must” differ across overloads.


Concept / Approach:

Overloaded functions may have the same number of parameters and still overload successfully if the types differ (e.g., void f(int) vs void f(double)). They may also have a different number of parameters (e.g., arity 1 vs 2). What they cannot do is rely solely on differing return types; the parameter list must differ. Therefore, the correct statement is that overloaded functions can accept the same number of arguments (with different types).


Step-by-Step Solution:

Example: void area(int r); void area(double r); → same count, different types. Example: void log(const char*); void log(const char*, int); → different counts. Attempt: int f(int); double f(int); → invalid (return type alone). Thus, count may be the same; types must differ in some way.


Verification / Alternative check:

Compile examples above: the first two pairs succeed; the last fails. This demonstrates the real constraints on overloading.


Why Other Options Are Wrong:

“Always return same type”: return type does not govern overloading.

“Only same number and same type”: identical signatures would be redefinitions, not overloads.

“Only different number and different type”: too restrictive; types alone can differ with same count.


Common Pitfalls:

  • Forgetting that default arguments can interact with overloads and cause ambiguity.
  • Depending on implicit conversions that make overload resolution surprising—prefer distinct signatures.


Final Answer:

Overloaded functions can accept same number of arguments.

More Questions from Functions

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion