Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialmohan Abdul
Courses Plus Student 1,453 Pointswhy isn't the output 7, instead the output is 5.
why isn't the output 7, instead the output 5
5 Answers
Sean T. Unwin
28,690 PointsThe variable, num
is printed before the addition is done. Essentially, it's passing the initial value of the parameter passed to the function.
Kyle Salisbury
Full Stack JavaScript Techdegree Student 16,363 PointsYeah, I understand your confusion, I'm not sure why she taught it like this. She needed to Print the called function, and not just call the function to make things more clear. Hopefully this will help. I'll try to break it down line by line.
def add_two(num):
print(num) # not important. She was just trying to prove that num equals the value of what ever you passed into it.
val = num + 2 # important code. what ever number we decide to pass (5 in our case), add two to it and call that variable val.
return val # This then takes that stored variable value (7 in our case) and saves it in the add_two function ready to be called.
add_two(5) #When you call a function, it does not show it's value it stores. You have to print it if you want to see it.
So when you call a function like she did add_two(5)
, it doesn't show you the value, it just processes it. The only way to show the value is if you were to print the function like this: print(add_two(5))
. This would have done two things. It would first show the number 5, only because we have the code in there that says print(num)
and we made num=5. However what we really care about is the value the function created. So the second thing it would have done is show the number 7.
So if we were to delete that print function inside the add_two function, we would have had code that looked like this:
def add_two(num):
val = num + 2
return val
print(add_two(5))
Output:
7
I hoped this helped.
Michał Hewelt
4,416 PointsCan I downvote a video ?
Fran Canete
Web Development Techdegree Student 15,964 PointsI can't print the variable "val". If it's a variable, shouldn't you be able to print it?
''' def add_two(num) val = num + 2 return val add_two(5) print(val) '''
Laurie Dunn
1,659 PointsHi Fran,
If you are trying to print 'val' outside of your function, it will not be able to see 'val'. See code below:
def add_two(num):
val = num + 2
return val
print(val)
The code above would provide the following error:
NameError: name 'val' is not defined
This is because you're printing from the global scope which can't see the local scope of the function.
You could print the 'val' inside the function like this (although this is not the purpose of this educational video):
def add_two(num):
val = num + 2
print(val)
return val
print(add_two(5))
This would print the number 7 twice because we are calling the function to print which adds 'num' (which we assigned as 5) and '2', then prints the 'val' sum that we just did while still inside the local scope of the function and then returns the 'val' to the function name 'add_two'. Then it prints our function.
What the function example used does, is return the value of the completed function to its name. Therefore you would want to print the name of the function for it to show the answer to the function:
def add_two(num):
val = num + 2
return val
print(add_two(5))
This will then print the result of our passed argument (which is a number in this case) to the 'val' variable of our function and then return 'val' variable to the name of our function.
I hope this was clear and helps!
William Hall
19,502 Points"who's on first... i don't know... third base. " - my brain after this video
GAOTLHOBOGWE GAOTLHOBOGWE
1,164 PointsGAOTLHOBOGWE GAOTLHOBOGWE
1,164 PointsThanks for the explanation Sean T. Unwin, I was wondering the same as mohan Abdul