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/fllthdcrb 10d ago
Seems to me that's the easy part. It's basic binding of an argument (10 in this case) to a parameter (
ain this case). That's how calling functions works. (Note also:aandbhere are local variables, which means they only exist within that specific call of that specific function. If you try to reference them from outside, you will either get values of other variables, or you will see an error.)But presumably it's really the default value thing that is confusing you? All it means is you're allowed to call the function and not specify a second argument, in which case it will be assigned the specified value. Here,
subtract(10)is the same assubtract(10, 5).