PCEP-30-02 · Question #221
Consider the following code. ``python def func(x=0): return x `` With sentence describes the function the best? A function defined like function()...
The correct answer is C. may be called without any argument, or with just one. Option C is correct because x=0 is a default parameter - Python allows you to call func() with no argument (using the default 0) or with exactly one argument like func(5), overriding the default. Why the distractors are wrong: A is wrong - the function never requires an…
Question
def func(x=0):
return x
With sentence describes the function the best?
A function defined like function()...Options
- Amust be called with exactly one argument.
- Bmay be called with any number of arguments (including zero).
- Cmay be called without any argument, or with just one.
- Dmust be called without arguments.
How the community answered
(43 responses)- A2% (1)
- B12% (5)
- C79% (34)
- D7% (3)
Explanation
Option C is correct because x=0 is a default parameter - Python allows you to call func() with no argument (using the default 0) or with exactly one argument like func(5), overriding the default.
Why the distractors are wrong:
- A is wrong - the function never requires an argument since a default is provided.
- B is wrong -
func(1, 2)would raise aTypeError; the function only accepts zero or one argument, not any number. - D is wrong - you can pass an argument;
func(42)is perfectly valid.
Memory tip: Think of the = in x=0 as a "safety net" - if no argument is thrown, the net catches it with the default value. A default parameter makes the argument optional, not forbidden and not unlimited.
Community Discussion
No community discussion yet for this question.