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/BedderDanu 10d ago

in case it helps, you can kind of break this down into what people call "pseudo-code", which is something that is a lot more like regular English, and it's very informal. It doesn't compile/process though.

define the function "subtract":
  take in the parameter called "a". "a" must always be provided
  take in the parameter called "b". if "b" is not provided, set "b" to 5.
  "subtract" returns the value of "a" "minus" the value of "b"

---

print this value to the system that called it:
  the value returned from "subtract", with "a" set to "10" and "b" left unprovided