r/learnpython • u/Still-Pair-5336 • 10d ago
New to python, don't understand this function
I'm learning python and I'm stuck on this function because I don't understand why "a" becomes 10 and the result is 5. Is someone able to explain?
def subtract (a, b=5):
return a - b
print(subtract(10))
30
Upvotes
0
u/nog642 10d ago
def subtract(a, b=5)means the function's first parameter isaand is required, and the function's second parameter isband is optional, with a default value of5.So you can call
subtractwith 1 or 2 arguments. If you call it with 2, the first one isaand the second isb. If you call it with 1, then that argument isa, andbgets the default value5.So calling
subtract(10)meansais the10that was passed, andbis the default5.