Explanation:
๐งฉ Function Definition
def f(x, y=5):
return x + y
def is used to define a function named f.
The function takes two parameters:
x → required argument
y → optional argument with a default value of 5
return x + y means the function will output the sum of x and y.
▶️ First Function Call
print(f(3))
Only one argument (3) is passed.
So:
x = 3
y = 5 (default value is used)
Calculation:
3 + 5 = 8
Output:
8
▶️ Second Function Call
print(f(3, None))
Two arguments are passed:
x = 3
y = None (explicitly provided, so default is NOT used)
Now the function tries:
3 + None
⚠️ This causes an error because Python cannot add an integer and NoneType.
❌ Error Produced
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
Final Output:
Error

0 Comments:
Post a Comment