Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Now that you've learned to create functions, learn to execute them by calling them.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Calling a function is what
we do when we wanna use it.
0:00
When your code is executed,
0:03
it's interpreted from top to bottom,
line by line.
0:05
If the interpreter encounters a function
call, it will jump to that function,
0:08
execute all the code inside of it, and
then go back to its previous place.
0:12
This means that you can execute
the code inside a function
0:17
as many times as you want without having
to rewrite all that code or copy and
0:19
paste a bunch of code
into the correct place.
0:23
Instead, all you have to do is call
the function anytime you want to
0:26
use the code instead of it.
0:29
The syntax for
0:31
calling a function is the functionβs name
followed by a set of parens, thatβs it.
0:31
Letβs pick up where we left
off in the previous video.
0:37
We wrote a function called
print_favorite_movie that you can see here
0:40
in my workspace.
0:42
This is a good name for
0:44
a function, because it accurately
describes what the code inside is doing.
0:45
When you, or another person, are reading
your code later on, you will know exactly
0:49
what that function does without having to
familiarize yourself with the code inside.
0:53
I can't emphasize enough how
important it is to write clear and
0:58
direct function and variable names.
1:01
I'll call the function now in
the same script file as the function.
1:04
Then I'll save the file, and I'll move
down into the terminal where I'll run it.
1:13
To run the file, I type python3,
which starts up the interpreter, and
1:19
then the name of the file, so
it knows what's it running, and
1:23
the name of our file here is functions.py.
1:26
Then I hit Enter.
1:31
Any output from the code
will be displayed below.
1:32
Cool, it printed out Mean Girls,
my all time favorite movie.
1:36
All right, now, you try.
1:40
Before moving on to the next video,
call your print_favorite_movie function in
1:41
your workspace, then save it and
run it down in the terminal.
1:45
When you've gotten this to work, and
1:49
you can see the function output in your
terminal, move on to the next step.
1:50
If you're having trouble, go ahead and
ask a question in the Treehouse community.
1:54
This is a great place
to practice asking and
1:58
answering questions, and
it helps make you a better developer.
2:00
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up