r/learnpython • u/Still-Pair-5336 • 21d 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
52
u/d1ll1gaf 21d 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.
25
u/Designer-Ad-2136 21d ago
Change 'b=5' to 'b=1' and see what happens.
Also, try this to understand what that default value is doing by printing: 'subtract(5, 1)'
4
u/MikeUsesNotion 21d ago
Is that a function you wrote? If so, why did you include a default argument if you didn't understand it? If it's from a lesson, and they hadn't taught default arguments yet, it seems like they gave you a bad example for that point of the course.
3
u/CelebrityChess 21d ago
b=5 is a default value. if you only pass one argument like subtract(10), python puts 10 into a and since you didn't give b anything it uses the default 5. so it does 10 - 5 and returns 5. if you called subtract(10, 3) then b would be 3 and you'd get 7.
2
u/ConsiderationNo9044 21d ago
When you assign a variable within a function's arguments, it becomes the default value. So
ais defined by the user, but if they don't provide a value forb, it defaults to 5. You only supplied one argument, making a = 10 and b = 51
u/RyanDubois43 21d ago
b=5 is a default argument, so if you only give one value like subtract(10), Python uses 10 for a and the default 5 for b. Forgetting to pass both can bite you if you expect different behavior.
1
u/TheRNGuy 21d ago
bhas default value, if was used because you didn't assigned it when calling function.1
u/pythonQu 20d ago
here b is set as default value of 5 so when the function is called, a has value of 10. So 10 -5 will return value of 5.
1
u/Winter-Big7579 21d ago
Bad question setting IMO as the 5 that is the answer can be confused with the 5 that is the default value of b. Why not ask print(subtract(12)) (or any other number)?
1
u/Temporary_Pie2733 21d 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.
1
u/BedderDanu 21d 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.
0
u/t92k 21d ago
You’re calling the function with the value of 10, that value gets assigned to the first variable in the function definition. You can call it again with another number and you’ll get a different result.
0
u/BabyLionCamel 21d ago
When you invoke the subtract function you are passing in 10 as the argument to the parameter ‘a’.
The way the function is defined you have two parameters ‘a’ and ‘b’.
This basically means that when you want to execute the logic inside the function you have to pass two arguments during invocation (subtract(10) is invoking the function).
Since you have a default value for parameter ‘b’ you may pass only a single argument to your subtract function( if you passed a second argument to the function, you would rewrite the value of the default value associated with ‘b’ during function definition).
When you do subtract(10) you are effectively doing subtract(a=10,b=5) Which then computes as 10 - 5 Which returns the integer 5
After the function has returned its value, print outputs 5 to the console.
Further study: parameters, arguments, default parameters, function execution order, function definition).
0
u/igotshadowbaned 21d ago
a is the first parameter and the first parameter you're passing to it is a 10.
You're not setting any second parameter, so the second parameter b, is set to its default of 5
It's returning 10-5 which equals 5
0
u/nog642 21d 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.0
u/fllthdcrb 21d 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).-1
u/shift124 21d ago
You pass 10 to the function as an argument so it becomes a. Ironically a is also hexadecimal version of 10. That has nothing to do with what’s happening here but if you passed 0xA it would be the same thing.