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

Python Functions, Packing, and Unpacking Getting Info In and Out of Functions Functions Recap and Cheat Sheet

Difference between def function(parameter) and function()

I just wonder what makes the difference in answering either one. In another words, what is the meaning behind those two separate formatting?

1 Answer

boi
boi
14,242 Points

Take a look at this example 👇

def print_my_name(parameter):
    print(parameter)

Going into the REPL

>>> print_my_name("Brock")

#Output "Brock"

Now using no parameter

def print_my_name():
    print("Brock")
>>> print_my_name()

#output "Brock"

Conclusion: The parameter format is used when you want to exclusively give input to your code, otherwise, if you know what the input will be, you just put it directly inside of the code block like in the above example.

Note: There are other more advanced reasons for each format (But since you are a beginner, leave it for later), this example gives a basic idea of how these formats work.