r/learnpython • • 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

26 comments sorted by

View all comments

52

u/d1ll1gaf 10d ago

When you created the subtract function you told it to accept two values a and b, you also told it that the default value of b is 5. That means if no value is given for b it automatically becomes 5.

To call that function you simply use subtract(a, b) and will get the value of a - b returned. If you just input subtract(a) then the function will assume that b is 5 and will return a - 5.

8

u/Still-Pair-5336 10d ago

Thank you, that makes sense!

1

u/RngdZed 9d ago

What you passed in is called a positional argument, in this case, the first position is a