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 trial

Python Functions, Packing, and Unpacking Getting Info In and Out of Functions Arguments and Parameters

mohan Abdul
PLUS
mohan Abdul
Courses Plus Student 1,453 Points

why 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
Sean T. Unwin
28,690 Points

The variable, num is printed before the addition is done. Essentially, it's passing the initial value of the parameter passed to the function.

Thanks for the explanation Sean T. Unwin, I was wondering the same as mohan Abdul

Kyle Salisbury
seal-mask
.a{fill-rule:evenodd;}techdegree
Kyle Salisbury
Full Stack JavaScript Techdegree Student 16,363 Points

Yeah, 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.

Can I downvote a video ?

I 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) '''

Hi 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!

"who's on first... i don't know... third base. " - my brain after this video