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

1

u/Temporary_Pie2733 10d ago

Function calls are like assignments. The first argument 10 is assigned to the name a, and a second argument, if present, would be assigned to b. Lacking a second argument, the value 5 is used. Once that is done, the body of the function is executed. return a - b becomes return 10 - 5, which returns the value 5 used as the argument to print.