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 trialAntonio Rodrigues
1,218 PointsWhy do we call the def calculate_price(number_of_tickets) instead of calculate_price(num_tickets)
def calculate_price(number_of_tickets):
return(number_of_tickets * TICKETS_PRICE + SERVICE_CHARGE)
6 Answers
Steven Parker
231,248 PointsThe "def" keyword indicates that the function is being defined (created). It doesn't cause it to run.
Later, when you use the function name without the "def", the function is being "called" (or "invoked"). This is when the code in it actually runs.
FYI: "return" is not a function, so you don't need parentheses around the value
Brent Capuano
949 PointsSo basically it doesn't matter what you put in () when defining calculate_price(number_of_tickets)?????
so i could write def calculate_price(butternut_squash) and later when I call the function, calculate_price(num_tickets)???
This makes no sense. why don't the things in parenthesis after calculate_price have to match???
Steven Parker
231,248 PointsYou're right, the parameter name can be anything, just as long as it is used consistently in the body of the function. But it is considered good programming practice to choose a name that reflects the kind of thing that the argument will represent. So perhaps a better choice would be something like "def calculate_price(purchased_item)
".
ad15
2,317 PointsA reason for changing the name of the parameter instead of using the same name as given to the variable, could be so that it's clearer when calling the same function but using a different variable for it. In this specific program it is not the case, so using the same name for the variable and the parameter isn't confusing - but in other cases it could be.
Mustafa Mohamed
8,408 PointsI think the teacher misspelled number_of_tickets with num_tickets, just use number_of_tickets when calling the function. It only worked for me that way.
Hanna Han
4,105 PointsI have tried to have like following,
def calculate_price(num_tickets): return(num_tickets * TICKETS_PRICE + SERVICE_CHARGE)
and it worked though. Shouldn't it work actually?
Steven Parker
231,248 PointsAs I said, the parameter name does not matter, as long as it is used consistently in the body of the function.
Jimmy Morgan
18,237 PointsI generally use "x" when defining a function. It helps me remember that its simply a placeholder for a real value, like in algebra.
def calculate_price(x): return(x * TICKET_PRICE + SERVICE_CHARGE)
Antonio Rodrigues
1,218 PointsAntonio Rodrigues
1,218 Pointsbut how is the number_of_tickets related if num_tickets?
Steven Parker
231,248 PointsSteven Parker
231,248 PointsWhile the function is being defined, "number_of_tickets" is a parameter, which serves only as a placeholder for the actual value that will be used when the function is called later.
Then, when the function is called, "num_tickets" is supplied as the argument, which replaces "number_of_tickets" while the code runs.