C++ function pointer to a function with defaults: what does Note(30) print when it calls Look(x) with a missing second argument?\n\n#include<iostream.h>\ntypedef void(*FunPtr)(int);\nint Look(int = 10, int = 20);\nvoid Note(int);\nint main()\n{\n FunPtr ptr = Note;\n (*ptr)(30);\n return 0;\n}\nint Look(int x, int y)\n{\n return (x + y % 20);\n}\nvoid Note(int x)\n{\n cout << Look(x) << endl; // calls Look(x, 20)\n}

Difficulty: Easy

Correct Answer: 30

Explanation:


Introduction / Context:
This question verifies that default arguments are associated with the callee declaration, not with pointers, and that they are applied when omitted at the call. The function pointer invokes Note, which calls Look(x) with only one explicit argument, letting the default fill the second parameter.


Given Data / Assumptions:

  • Look is declared as int Look(int = 10, int = 20);
  • Note(int x) calls Look(x); thus y defaults to 20.
  • Operator precedence: x + y % 20 means x + (y % 20).


Concept / Approach:
When Look is called with a single argument, the default y = 20 is used. Because y % 20 = 0, the return becomes x + 0 = x. The pointer indirection (*ptr)(30) simply routes to Note(30) and does not change default-argument behavior.


Step-by-Step Solution:
1) (*ptr)(30) ⇒ calls Note(30). 2) Note calls Look(30) ⇒ y defaults to 20. 3) Compute Look: 30 + (20 % 20) = 30 + 0 = 30. 4) Note prints 30.


Verification / Alternative check:
If Note were cout << Look(x, 5), the result would be x + (5 % 20) = x + 5.


Why Other Options Are Wrong:
10/20/40 assume different argument counts or precedence; compilation succeeds as signatures are consistent.


Common Pitfalls:
Assuming defaults are tied to function pointers or misreading the modulo precedence.


Final Answer:
30

More Questions from Functions

Discussion & Comments

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