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 Introduction to Functions Defining a Function

I am used to seeing: print(favorite_movie) not print_favorite_movie() Which is the more widely used convention?

Just wondering how old this video is

3 Answers

Szymon Dabrowski
Szymon Dabrowski
2,207 Points

So in the first example we are creating a variable called favorite_movie which contains a string that is: "Deadpool". Then we use the print() command to use the variable which, prints out "Deadpool".

In the second example we are creating a function called print_favorite_movie(): that takes no other arguments inside the parenthesis. This function is a name you make up so it could even be favorite_movie or movie however, the first example explains what the function does much better. To create a function we use the def keyword first then, the name you make up then, parenthesis and, finally a colon which indicates that the function is starting. In this example we simply define our function to use the command print() to print out "Deadpool" (don't forget to indent 4 lines when writing in a function). Finally, outside of the function we just write the functions name print_favorite_movie() and, this basically runs the code inside of it which is print("Deadpool").

So to summarize they both output the same thing. So why would you use a function instead? Functions are a great way to help us avoid writing the same code over and over because; we can just call the function whenever we need to execute that code. Functions make your code easy to read and test. You can still write code without any functions but if there is a bug then it will be very difficult to find let alone solve it! as well as, others will have a hard time reading your code.

Tip: You should be able to think of a short and descriptive name for your function. if you cant do that the function is probably doing too much.

Example 1:

favorite_movie = "Deadpool"

print(favorite_movie)

Output: Deadpool

Example 2:

def print_favorite_movie():
    print("Deadpool")

print_facorite_movie()

Output: Deadpool

I hope this helps or explains your question better!

Hi Suzanne

In this video with print_favorite_movie() the teacher is calling the function. As she explains in the next video this is a good name for a function because it accurately describes what the code inside is doing. When she calls the function the output is 'Mean Girls'.

marin stefan daniel
marin stefan daniel
5,422 Points

< print(favorite_movie) > is a command that prints whatever is stored into 'favorite_movie' . it calls a function called 'print' which comes as built in an. < print_favorite_movie() > is a command that calls (executes) a previously defined function (by the user probably ..certainly not built in ) called ,in this case, 'print_favorite_movie'