r/learnpython • u/Still-Pair-5336 • 10h 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))
23
u/Designer-Ad-2136 10h 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)'
13
u/Still-Pair-5336 10h ago
Ahhh, so when I enter 'subtract(5, 1)', it essentially means a=5 and b=1, which returns a value of 4?
17
u/juanfnavarror 9h ago
Yes. Good tip is to think “how can I answer this question writing code”. You would be able to verify this by tweaking. Also useful to look in the official python reference material.
1
3
u/MikeUsesNotion 10h 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/Still-Pair-5336 9h ago
It was a test with multiple choice, asking me what value it would return. It really confused me since the code seemed so different what we had been taught
2
u/ConsiderationNo9044 10h ago
When you assign a variable within a function's arguments, it becomes the default value. So a is defined by the user, but if they don't provide a value for b, it defaults to 5. You only supplied one argument, making a = 10 and b = 5
2
u/CelebrityChess 6h 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.
1
u/RyanDubois43 8h 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/Winter-Big7579 5h 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/BedderDanu 5h 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 unprovided0
u/Temporary_Pie2733 10h 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.
0
u/BabyLionCamel 10h 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/shift124 9h 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.
0
u/igotshadowbaned 9h 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 9h ago
def subtract(a, b=5) means the function's first parameter is a and is required, and the function's second parameter is b and is optional, with a default value of 5.
So you can call subtract with 1 or 2 arguments. If you call it with 2, the first one is a and the second is b. If you call it with 1, then that argument is a, and b gets the default value 5.
So calling subtract(10) means a is the 10 that was passed, and b is the default 5.
0
u/fllthdcrb 9h ago
why "a" becomes 10
Seems to me that's the easy part. It's basic binding of an argument (10 in this case) to a parameter (a in this case). That's how calling functions works. (Note also: a and b here 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 as subtract(10, 5).
29
u/d1ll1gaf 10h 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.