Which of the following Function Headers in Python code will result in error?
2023
Which of the following Function Headers in Python code will result in error?
- A.
def Compu(X, Y=10, Z=100):
- B.
def Compu(X, Y, Z=1):
- C.
def Compu(X=100, Y=10, Z=1):
- D.
def Compu(X, Y=10, Z=1)
Attempted by 1497 students.
Show answer & explanation
Correct answer: D
Answer summary: Only option D will result in an error.
In Python, every function definition header must end with a colon (:). Option D is written as def Compu(X, Y=10, Z=1) without the trailing colon, so it raises a SyntaxError.
Options A, B, and C are valid:
A. def Compu(X, Y=10, Z=100): is valid because the non-default parameter X appears before parameters with default values.
B. def Compu(X, Y, Z=1): is valid because X and Y are non-default parameters followed by the default parameter Z.
C. def Compu(X=100, Y=10, Z=1): is valid because all parameters have default values.
Therefore, the correct answer is option D.