If a function has three parameters, which position(s) may legally have default arguments under standard C++ rules? Choose the most accurate statement.

Difficulty: Easy

Correct Answer: The trailing argument will be the default argument.

Explanation:


Introduction / Context:
Default arguments must be placed so that call sites remain unambiguous. The conventional rule is that once a default is introduced, all parameters to the right must also have defaults. This governs which positions can legally carry defaults when some parameters remain non-defaulted.


Given Data / Assumptions:

  • There are exactly three parameters.
  • We are considering cases where at least one parameter has no default.
  • Standard C++ constraints apply.


Concept / Approach:
If any parameter lacks a default, then any defaults must appear only after that parameter. Hence, the rightmost (trailing) parameter is the safe and typical candidate for a default when earlier parameters remain non-defaulted. Alternatively, it is legal to default all three, but not legal to default a middle parameter while leaving a trailing parameter without a default.


Step-by-Step Solution:
1) Legal: void f(int a, int b, int c = 0); // trailing default2) Also legal: void f(int a, int b = 1, int c = 0); // trailing block of defaults3) Illegal: void f(int a = 1, int b, int c = 0); // non-trailing b breaks the rule4) Therefore, the most accurate general statement is that the trailing argument is (may be) the default one.


Verification / Alternative check:
Compile the examples to observe success for trailing defaults and failure for the middle-default case with a non-default trailing parameter.



Why Other Options Are Wrong:
First-only default with later non-defaults violates the trailing rule.Middle-only default with a non-default trailing parameter is ill-formed.“All arguments” can be defaulted, but that statement is not the best characterization when the question contrasts single-position cases; the safe, general choice is the trailing position.



Common Pitfalls:
Assuming any parameter can be defaulted in isolation; remember the trailing-block rule for defaults.



Final Answer:
The trailing argument will be the default argument.

More Questions from Functions

Discussion & Comments

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