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
Brent Capuano
949 Pointscalculate_price(x)
In this lesson we refactored an equation, num_tickets * ticket price to calculate_price(num_tickets)
then we defined calculate_price in the beginning as: def calculate_price(number of tickets)
Why is it ok to have different items in the parenthesis following calculate_price? num_tickets does not = number_tickets but the program still works fine with different variables in ()'s.
So could I write whatever I want when defining calculate_price? For instance def calculate_price(apple_jax) And then later when I call it type calculate_price(num_tickets)?
2 Answers
Eric M
11,547 PointsYou've got the idea. In short, yes.
To get into a bit more detail, calling a function and defining a function are different things. When we define a function we use the def keyword to specify that we're writing a function definition, we give the function a name, and we provide the function with names for any parameters that we expect. Then the colon indicates the function signature (name and params) is complete and the function itself follows.
def function_name(parameter_one, parameter_two):
Within the function, we refer to the parameters by their parameter name.
Outside of the function definition we call a function with its name and any arguments that it requires. We say that arguments are passed to the function.
some_variable = function_name(argument_one, argument_two)
Sometimes we won't even know how many parameters we might need! It's possible to write functions in a way that handle as many arguments as are passed. Stick with the courses and you'll see how.
Often you'll see code where the arguments are named the same as the parameters, so long as they're in different scopes (distinctly different areas of the program) this is fine too. Sometimes it makes a lot of sense for arguments and parameters to have different names.
e.g.
price_apple = 1.20
price_banana = 2
cost_dozen_apples = calculate_price(12, price_apple)
cost_dozen_banana = calculate_price(12, price_banana)
def calculate_price(quantity, price):
return quantity * price
Dave StSomeWhere
19,870 PointsShort answer is yes you name it what ever you want.
When defining a function you can define 0 to many parameters and name them what ever you want and are positional.
So then, when calling the function you need to pass the same number of positional parameters (which is why there are features like unpacking and default values).
On the function declaration they are called parameters and when calling they are called arguments (although many people use the terms interchangeably)
In code:
# define a function
def example_function(parm1, parm2):
# here in the function you need to use the name per the function declaration
# so you can do stuff with the variables parm1 and parm1
pass
# when calling this example function you need to pass two arguments
# the names don't matter
some_variable = example_function(arg1, arg2)
Hope that helps
Philip Schultz
11,437 PointsPhilip Schultz
11,437 PointsCan you you post a link to the video? I think you're asking what the difference between an argument and parameter is, but I'm not too sure. In case this is the question, an argument is the variable that you put in parentheses when you call the function, this can be called anything and does not have to have the same name as the parameter for the function. A parameter is the variable in the actual function, it's the piece of information needed to run the function. A parameter gets reassigned to the value of the argument passed to it.