Which of the following function prototypes is perfectly acceptable in C++ for using a default argument? Assume referenced names are declared before the prototype.

Difficulty: Easy

Correct Answer: int Function(int Tmp = Show());

Explanation:


Introduction / Context:
Default arguments can be any valid expression that is type-checkable at the point of declaration and evaluable at the call site. This includes calling other functions, provided those functions are declared beforehand and the expression is well-formed.


Given Data / Assumptions:

  • The function Show() is declared and visible before the prototype using it as a default.
  • Default argument expressions must be syntactically valid and convertible to the parameter type.
  • We are comparing syntactic correctness of the options.


Concept / Approach:
Option A uses a valid expression: Show() is a function call with no arguments; its return value is used as the default for Tmp. The compiler type-checks this at the declaration and, if used, evaluates it at each call site where Tmp is omitted. Option B is ill-formed because it writes a type-list (Show(int, float)) in the default expression rather than an actual call with values, which is not valid C++ syntax. Option D is nonsensical and not a function prototype at all.


Step-by-Step Solution:
1) Check A: int Function(int Tmp = Show()); // valid if Show() is declared and returns a type convertible to int2) Check B: float Function(int Tmp = Show(int, float)); // invalid; default must be an expression, not a type signature3) Check D: not a valid prototype in C++ syntax.4) Therefore, only A is perfectly acceptable.


Verification / Alternative check:
Compile with a proper declaration of Show(): int Show(); and observe that Option A compiles and permits calls like Function(); which evaluates Show() at the call site for the omitted parameter.



Why Other Options Are Wrong:
B uses a type-spec list instead of an expression; it will not compile.D is malformed and not recognized as a function declaration.C is false because B and D are not valid; only A is acceptable.



Common Pitfalls:
Using undeclared names in default expressions (name lookup happens at the declaration) or repeating defaults across multiple declarations leading to ODR issues.



Final Answer:
int Function(int Tmp = Show());

More Questions from Functions

Discussion & Comments

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