C++ default arguments usage: in what effective order do omitted arguments get supplied at the call site?

Difficulty: Easy

Correct Answer: The order of the default argument will be right to left.

Explanation:


Introduction / Context:
Default arguments in C++ are applied to trailing parameters that the caller omits. Practically, this means you provide actuals for the leftmost parameters and let the compiler fill in the rest using the defaults. Many MCQs summarize this as “defaults are taken from right to left.”


Given Data / Assumptions:

  • Defaults are specified in the function declaration.
  • Callers may omit only a suffix of arguments.
  • Evaluation of defaults happens at the call site.


Concept / Approach:

Because only the rightmost parameters can be defaulted in a usable way, when a caller omits arguments, the compiler supplies defaults beginning with the rightmost parameter and moving leftward as needed. This is why declarations must place non-defaulted parameters before defaulted ones. Any attempt to default a middle parameter creates unusable or ambiguous calls and is ill-formed.


Step-by-Step Solution:

Consider f(a, b = B, c = C). Call f(x) → b takes B, c takes C (filled from right to left). Call f(x, y) → c takes C only; b uses y. Call f(x, y, z) → no defaults used.


Verification / Alternative check:

Compile examples where you omit only trailing arguments; successful calls confirm the right-to-left filling. Try skipping a middle parameter; compilation fails, reinforcing the trailing rule and right-to-left mental model.


Why Other Options Are Wrong:

Left to right: contradicts the trailing-parameter rule.

Alternate or random: defaults follow a strict, deterministic rule, not arbitrary order.


Common Pitfalls:

  • Defining defaults for non-trailing parameters.
  • Redeclaring defaults across multiple declarations.


Final Answer:

The order of the default argument will be right to left.

Discussion & Comments

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