C++ default arguments: which statement is correct about redefinition and using function calls as defaults?

Difficulty: Easy

Correct Answer: C++ does not allow the redefinition of a default parameter.

Explanation:


Introduction / Context:
Default arguments simplify function calls by supplying omitted values. Two frequent sources of confusion are whether the default can be a function call and whether a default can be specified again in another declaration. Clarifying these rules avoids ODR and ambiguity problems in headers and source files.


Given Data / Assumptions:

  • We may declare the same function more than once across translation units.
  • A default argument is substituted at the call site.
  • Expressions used as defaults must be visible at the point of declaration.


Concept / Approach:

C++ does allow a default argument to be any valid expression (subject to scope), including a function call such as int f(int x = get_default()); as long as the callee is declared beforehand. However, C++ forbids redefining the default for the same parameter in another declaration; you should place the default once (typically in a header) and omit it from later declarations/definitions. Thus, statement A is false, statement B is false, and statement D is the correct choice: redefinition of a default parameter is not allowed.


Step-by-Step Solution:

Check A: “cannot be function call” → false; function calls are allowed as defaults. Check B: “allows redefinition” → false; violates the one-default rule. Therefore D (no redefinition allowed) is the correct statement. C says both are true; it is therefore wrong.


Verification / Alternative check:

Write two declarations: one with = get_default() and a later one trying = 42. The compiler rejects the second. Ensure get_default is declared before the defaulted parameter; compilation then succeeds, proving A is false.


Why Other Options Are Wrong:

A: contradicts the language; defaults are ordinary expressions evaluated at the call site.

B: contradicts ODR-style rules for defaults.

C: a combination of two false statements.


Common Pitfalls:

  • Putting the same default both in the header and the definition file.
  • Using non-visible names in the default expression.


Final Answer:

C++ does not allow the redefinition of a default parameter.

Discussion & Comments

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