Defining a Function2:44 with Ashley Boucher
Create a function of your own.
Now that we know a little bit about what a function is, 0:00 let's look at how to write one. 0:02 Feel free to follow along from the attached workspace, but 0:04 don't get too hung up on typing or trying to copy me as I go. 0:07 There will be opportunities for you to pause the video and 0:10 try things out on your own. 0:13 Functions serve the same purpose in every programming language. 0:14 But the syntax for 0:17 writing them is going to be a little different from one language to the next. 0:18 In Python you know you've encountered a function when you see the keyword def 0:22 that's D-E-F. 0:26 Followed by a snake cased string, 0:27 which is the name of the function followed by a set of parentheses and a colon. 0:29 Snake casing is when you string together words using underscores instead of spaces. 0:33 Snake casing doesn't use any capital letters. 0:38 For example, my name written in snake case would be ashley_boucher. 0:41 If you want to name your function with multiple words, use snake casing. 0:45 This is the Python style standard. 0:50 Now I'm gonna write a simple function called print name to show you the correct 0:53 format for a function definition. 0:57 So start with the keyword, def, followed by the name of the function, 1:01 which is print_name in snake case, then some parens and a colon. 1:05 That colon at the end indicates the function is beginning. 1:09 What follows that colon will be the code block that gets executed once the function 1:13 is called. 1:16 The contents of the function must be indented four spaces. 1:18 In this case, the contents of the function is just one line that prints a string. 1:21 That's it. 1:29 Now it's your turn. 1:31 Pause the video here and in the attached workspace go ahead and 1:32 try to write your own function. 1:35 Name your function print favorite movie, and 1:37 in the body of your function print out the title of your favorite movie. 1:39 When you think you've got it, unpause the video to go over the solution. 1:43 All right, how did it go? 1:46 Were you able to write your function? 1:49 Let's take a look. 1:51 The first thing you should have is the keyword def. 1:52 The def keyword should be followed by the name of the function. 1:57 Did you remember to use snake case to write the name? 2:00 It'll look like this. 2:02 Then one of parens, followed by a colon. 2:05 Finally, to write the body of the function we add a new line, 2:08 indent four spaces, and print out the title of our favorite movie. 2:12 Don't forget to add quotes around the movie title to indicate that 2:21 it's a string. 2:23 Awesome work, now you know the basic structure for writing a function. 2:25 You have a new tool in your tool belt that is super critical to writing Python and 2:28 computer programming in general. 2:32 The next step is learning to use your function. 2:35 In other words, calling it so that the code inside actually gets executed. 2:37 Join me in the next video to learn how. 2:42
You need to sign up for Treehouse in order to download course files.Sign up