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 trialKlaudija Ljevar
4,476 PointsConfused about num_tickets and number_of_tickets
I need some help understanding. Why did Craig named: amount_due = calculate_price(num_tickets) and than in function it was different: def calculate_price(number_of_tickets). How come it still works , if you know what I mean?
8 Answers
Nemanja Savkic
17,418 PointsI understand it as just a name for the argument and it should be the same in the return line. You can then change the argument that you post inside. def func(x): return x * 3
func(4)
Alex Kravchenko
1,278 Points@Ryan and Anthony Bednarek, this may be too late but I was confused about this as well. When defining the function, you are putting in the parameters into those parenthesis. It is not the same variable that's in your code. You can name it whatever you want, you just have to put it there and then do something with those parameters below in the function. Once you are using the function in your code, the variables you are putting into the function are arguments. So the function would have worked fine if it was
def calculate_price (mickey_mouse)
return (mickey_mouse * TICKET_PRICE) + SERVICE_CHARGE
#then when the function is called:
final_price = calculate_price (num_of_tickets)
#num_of_tickets is what gets passed into the function
#is the mickey_mouse parameter which gets multiplied by TICKET_PRICE plus SERVICE_CHARGE
#and returns the total price.
The name you put into the parameters doesn't have to be logical or have anything to do with anything. You just put in the most obvious thing you can so you can know what the function is doing and which arguments (variables) will be passed into it. When using your functions, you can put whatever variables in it and it will be what goes into those parenthesis and ran within the function. I don't know if that made sense.
Rosanna Tan
2,058 PointsThank you, it was helpful!
Oren Schindler
2,166 PointsYa, this makes sense to me. (num_tickets) replaces (mickey_mouse) in the function, or whatever follows calculate_price(). So when calculate_price(num_tickets) runs, it returns (num_tickets * TICKET_PRICE) + SERVICE_CHARGE
Chris Freeman
Treehouse Moderator 68,441 PointsTo add a bit more detail. The names are different because they can be. The namespace of the function contains variables that are active within the function. This includes the parameter number_of_tickets
. The namespace of the program includes the variable num_tickets
which is used as an argument to the function.
These two namespaces are distinct precisely to decouple them. This allows the function freedom to use names without regard to what names are outside of a function. A very important feature since a function has no idea how it will be called and by what code. It simply takes the arguments and matches them to its parameter list (basically assigning its parameter name, or label, to point to the same object that the argument label points at.)
Mustafa Başaran
28,046 PointsHi Klaudijja, The logic is the same as simple algebra. A function may take an argument: f(x) and x may take a value: 4 so, it becomes f(4) Right?
Klaudija Ljevar
4,476 PointsThank you, Mustafa. Think it's clearer now :)
Iain Crawford
Courses Plus Student 396 PointsI think she means he has used the different names for the same variable.
anthonybednarek
1,466 PointsMustafa's answer doesn't really explain why he created both. Mine runs perfectly fine with just the num_of_tickets.
Angela Anders
1,844 PointsI don't really understand why the variable or argument had to change from num_tickets to number_of_tickets either. It does make it confusing. Anthony, You are correct. Mustafa didn't answer the question of why it changed. Why did it change?
Iain Crawford
Courses Plus Student 396 PointsI think Alex explained it better. Its the parameter (that goes in the brackets when you define a function), they dont matter, but the argument (that goes in when you call the function) does I think. The teacher should have explained this better. People here are calling these arguments, but they are parameters.
The paramaters are given in the function... def fun(parameter1) are then passed down to the argument when you call the function....: fun(num_tickets)
Ratan Singh
1,440 PointsHi everybody, the name of the argument could be anything, it would just work inside the function locally. If you want you can keep the same name as well. no issues But the real question is there is no need of argument. The program would work if we do not pass the argument. Why would he do that??? :(
ryan campbell
523 PointsIt's something that has confused me as well. If you happen to get an answer can you please post it here?
Klaudija Ljevar
4,476 PointsYou cannot see the answer from above? From Mustafa?
Iain Crawford
Courses Plus Student 396 PointsWould be better if the lecturer could explain the difference between paramenters and arguments. (parameters get passed down to the function when you call the function).
Federico Lemaire
8,423 PointsHi there everybody! I have an issue with this too. If I change num_tickets to number_of_tickets as the teachear does, I have return this error after I put how many tickets I wanna buy. Why this doesn'y happen to the teacher?
Traceback (most recent call last): File "/Users/fedelemairegmailcom/Desktop/Python/Ejercicios/theproject.py", line 19, in <module> amout_due = calculate_price(num_tickets) File "/Users/fedelemairegmailcom/Desktop/Python/Ejercicios/theproject.py", line 6, in calculate_price return num_of_tickets * TICKET_PRICE NameError: name 'num_of_tickets' is not defined
Federico Lemaire
8,423 PointsWell, I jump one row the while function and now runs ok 🤔