Welcome to the Treehouse Community
Looking to learn something new?
Sean Richardson495 Points
I'm trying to understand the role of the () in def my_function (): and how its used and when. My understanding so far is that its an argument and that's about it. If for example the function is def my_function (item): does that mean I have to declare what item is ie item = ['cat', 'dog'] to use in the function. And what about when nothing is shown in () in the def statement, how and when is that used. I can't seem to find much in treehouse.
Actually when defining a function the thing within the parenthesis is called a parameter, not an argument. Though arguments are related to this topic.
When you specify a parameter for a function you are stating that when the function gets called it needs to have a value passed in to it. And that said value will be assigned to that name within the function. That value by the way is what is referred to as an argument.
Take this example:
def my_function(word): # my_function takes has one parameter called word print(word) # Print the contents of the word parameter my_function("Hello, World!") # I'm calling my_function with one argument which is "Hello, World!"
The above code will result in "Hello, World!" being printed out. Specifying a parameter for your function makes it possible to pass data into the function when it is called, as demonstrated above. This is useful since it makes functions far more dynamic since they won't always work on the same data each time they are called.
Though it's worth noting that when you specify that your function requires a parameter as I show above then an argument has to be supplied when the function is called, not supplying an argument will lead to an error.