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
Jason Pallone
895 Pointspython arguments????
so if you go into the link above and go into shopping_list for example , we have def add(new_item): now in that function new item was NEVER defined? we actually defined it in def main(), and we use new_item all throughout the code? So how does a argument work? I thought it had to be defined in the function it is assigned to and then outside that function it is not the same defined? Like you can have new_item in def add and new_item in def main and both can be completely different? SO how does new_item get defined outside of its function and still be the same all throughout the code???? Arguments really confuse me :(
1 Answer
behar
10,800 PointsHey Jason! I agree this seems very confusing, but it has to do with the concept of scope in programming.
If you create a variable inside a function, that variable will only be accesible within the function itself, so an example would be:
myVariable = 1
def function():
myVariable = 2
print(myVariable)
This will actualaly print 1, and it seems weird right, because usually you would assume the second time you declare that variable, it would take the new value. But these are actually two seperate variables with the same name, and the variable that contains 2 is only accesable from inside the function.
Infact if you tried to remove the "myVariable" that is declared outside the function you would recive a NameError: name 'myVariable' is not defined. But if you moved that print statement inside the function, and then called the function, it would print just fine.
But that makes sense right, because now the print statement is within the scope of the variable.
To the second part of your question, I think what your trying to ask is why its possible to have a function look like this:
def function2(myVar):
print(myVar)
The "myVar" variable isnt ever defined so how can we print it? Well we do define it when we call the function, because the way we would call the function is like this:
function2("Hello world!")
# This would print "Hello world" to the console
So we define that variable when we pass it into the function. This will definetly be covered more later on.
Infact we can make function take multiple arguments like this:
def function3(var1, var2, var3, var4, var5):
print(var1)
print(var2)
print(var3)
print(var4)
print(var5)
Then we could call it like this:
function3("This", "is", "defining", "the", "variables")
# This would print:
# This
# is
# defning
# the
# variables
I hope this answers your question, please write back if you need more help tho!
Jason Pallone
895 PointsJason Pallone
895 PointsThank you this is helping! So for when the variable is part of the function like the above example Def function2(myvar):
So when you do function2(hello) it prints hello? You define the variable inside the () when you call the function?
Now for a example of what confuses me the most is this
So say we have
def function(one): two = 'hello world' print(two)
def new_function() one = input('whats up ')
function(one)so when i call function(one) there, is one defined as one = input('whats up ')??? even though it was a argument in the first function, but defined in the new function? I see this is code a lot and it is very confusing for me
Jason Pallone
895 PointsJason Pallone
895 Pointsthe function(one) and new_function, the code next to it is supposed to be below it, i dont know how to make that happen on this community page sorry for the confusion
behar
10,800 Pointsbehar
10,800 PointsHey again Jason! Check out the "markdown cheatsheet" below the "add an answer" to see how to format code properly, it is as you point out very difficult to see whats going on without it.
I think you pretty much got it! One thing to point out is that you always have to remember to put "" around a word if you want it to be a string. This is true for arguments aswell. If you do:
Later you go on to define two function, one is "function" and another is "new_function". Now you use a variable called "one" in both of them, in the first function you define that variable "one" in the arguments of the function, in the other you define it as what is returned from the input. And this is totally valid code, those variable share nothing but a common name in different "scopes" of your code.
So dont get confused when you see the same variable names used in different functions, theyre not reffereing to the same variable, but it was just convient to name them the same thing.
You can go mess around with this in the shell, and the concept of scope might seem confusing and inconvinient in the beginning, but i promise you it will become very helpfull later on.
One thing to point out with your example is that you make a function look like this:
But there is no point in letting this function take an arguement, because you dont use it in the function. You could call this function with anything, and the function would still just print "hello world".
A more valid reason to make a function take an arguement is if your actually want the user to be able to change how the function behaves, as demonstrated earlier, this is a beginner friendly function:
Now you can go ahead and pass whatever you want into that function and it will print it to the console:
Hope you got it now, but dont fear to write back if you still need more help, altough make sure to use the "markdown cheatsheet" if you wanna write some code!
Jason Pallone
895 PointsJason Pallone
895 PointsFirst off thank you so much for taking the time to help me this is really helpful for me. Thank you so much!
So for the above is my example, I am confused as to what def add(new_item) why new_item was a argument? It was defined in the main() so how did that work? This is where im mainly confused with the arguments because it wasn't defined in the same function it is a argument in, but it is used in that function and other functions?
behar
10,800 Pointsbehar
10,800 PointsAh i think i see why thats confusing you Jason! I'll break it down:
So just to be clear, everything i said holds true. So yes, there are two variables named new_item. One is an argument that is passed into the add function, and one that is defined to be whats returned from the input in the main function.
Now as explained earlier, these are two completely seperate variables in two different scopes of the code, and they are only accesable from within the function in which they were declared.
Remember that a function wont run untill we call it. So the main and add function dont run before we tell them to. Now if you look at the bottom of the code snippet, you will see that we are calling the main function like this:
main()Now we dont pass any arguments to main, because it dosent take any right? Now what happens when we run main is that it will ask us for that input, and what is returned will be stored inside the variable new_item. But only the main function knows about this variable, so if we did nothing more and tried to run the add function outside the main function with the variable new_item, we would get a NameError because outside the main function, "new_item" does not exist.
Now assuming that the input we typed is not equal to the keywords: 'done', 'show', 'help' or 'remove', we will reach that else block at the bottom of main, which calls that add function, with the variable new_item.
So as explained earlier, we can only do this from within the main function, because it is the only function that knows about the variable new_item.
So we call the add function, with the variable new_item, which could contain say: "bananas". So bananas is now the argument put into the add function. Now the original variable "new_item" from the main function, is a COMPLETELY seperate variable from that inside the add function, that just happens to contain the same value becuase we called it with a variable containing "bananas", and happens to be named the same thing.
Infact, in you want to, you can make this code work EXACLY the same, if you go ahead and change the argument name inside the add function to whatever name you like:
The above code works exacly the same, no difference whatsoever, i just changed the argument name used in the add function.
So just to be clear, in the else block inside the main function, we call add with the variable "new_item", which in our example contains "bananas", now the value that "new_item" contains is stored within the entirely new variable called not_new_item. Right? So they share nothing whatsoever but a common value.
That was also the case before we changed "new_item" to "not_new_item", they just so happend to have the same name, and just so happens to contain the same value, but were still two entirely seperate variables. But since we knew they were actually refferencing the same value, it just made sense to call them the same thing, so that it would be obvious to someone reading our code, that the two ENTIRELY seperate variables, were refferecing the same value.
So just an example:
But remember, were not printing "variable1", we are printing an entirely seperate variable, that just so happens to contain the same value as "variable1" has.
So exacly the same happens here:
But even tho there are two variables called "myVariable", they are two seperate variables, in two different scopes of your code, that just happens to contain the same value, and happens to be named the same thing.
So to recap all of that, remember that whatever we call the arguements we can put inside a function, it does not have to be named the same thing as what we pass into it, it is just a variable, that can contain anything, and because of scope, we can name it something that have already been used in other functions.
And btw this confused me alot aswell, so dont think this is just you :D!
Jason Pallone
895 PointsJason Pallone
895 PointsThank you so much for all the help! I think I understand it all now and it does make sense! Thank you for taking the time to break it down with me it was very nice and helpful Thank you!!