1 00:00:00,350 --> 00:00:02,620 Now that we know a little bit about what a function is, 2 00:00:02,620 --> 00:00:04,570 let's look at how to write one. 3 00:00:04,570 --> 00:00:07,070 Feel free to follow along from the attached workspace, but 4 00:00:07,070 --> 00:00:10,410 don't get too hung up on typing or trying to copy me as I go. 5 00:00:10,410 --> 00:00:13,058 There will be opportunities for you to pause the video and 6 00:00:13,058 --> 00:00:14,337 try things out on your own. 7 00:00:14,337 --> 00:00:17,670 Functions serve the same purpose in every programming language. 8 00:00:17,670 --> 00:00:18,491 But the syntax for 9 00:00:18,491 --> 00:00:22,012 writing them is going to be a little different from one language to the next. 10 00:00:22,012 --> 00:00:26,548 In Python you know you've encountered a function when you see the keyword def 11 00:00:26,548 --> 00:00:27,700 that's D-E-F. 12 00:00:27,700 --> 00:00:29,484 Followed by a snake cased string, 13 00:00:29,484 --> 00:00:33,492 which is the name of the function followed by a set of parentheses and a colon. 14 00:00:33,492 --> 00:00:38,350 Snake casing is when you string together words using underscores instead of spaces. 15 00:00:38,350 --> 00:00:41,480 Snake casing doesn't use any capital letters. 16 00:00:41,480 --> 00:00:45,966 For example, my name written in snake case would be ashley_boucher. 17 00:00:45,966 --> 00:00:50,820 If you want to name your function with multiple words, use snake casing. 18 00:00:50,820 --> 00:00:53,172 This is the Python style standard. 19 00:00:53,172 --> 00:00:57,049 Now I'm gonna write a simple function called print name to show you the correct 20 00:00:57,049 --> 00:00:58,820 format for a function definition. 21 00:01:01,090 --> 00:01:05,257 So start with the keyword, def, followed by the name of the function, 22 00:01:05,257 --> 00:01:09,157 which is print_name in snake case, then some parens and a colon. 23 00:01:09,157 --> 00:01:13,118 That colon at the end indicates the function is beginning. 24 00:01:13,118 --> 00:01:16,982 What follows that colon will be the code block that gets executed once the function 25 00:01:16,982 --> 00:01:18,250 is called. 26 00:01:18,250 --> 00:01:21,210 The contents of the function must be indented four spaces. 27 00:01:21,210 --> 00:01:25,035 In this case, the contents of the function is just one line that prints a string. 28 00:01:29,216 --> 00:01:31,830 That's it. 29 00:01:31,830 --> 00:01:32,694 Now it's your turn. 30 00:01:32,694 --> 00:01:35,628 Pause the video here and in the attached workspace go ahead and 31 00:01:35,628 --> 00:01:37,770 try to write your own function. 32 00:01:37,770 --> 00:01:39,977 Name your function print favorite movie, and 33 00:01:39,977 --> 00:01:43,385 in the body of your function print out the title of your favorite movie. 34 00:01:43,385 --> 00:01:46,880 When you think you've got it, unpause the video to go over the solution. 35 00:01:46,880 --> 00:01:49,416 All right, how did it go? 36 00:01:49,416 --> 00:01:51,400 Were you able to write your function? 37 00:01:51,400 --> 00:01:52,553 Let's take a look. 38 00:01:52,553 --> 00:01:54,887 The first thing you should have is the keyword def. 39 00:01:57,370 --> 00:02:00,270 The def keyword should be followed by the name of the function. 40 00:02:00,270 --> 00:02:02,539 Did you remember to use snake case to write the name? 41 00:02:02,539 --> 00:02:05,494 It'll look like this. 42 00:02:05,494 --> 00:02:08,599 Then one of parens, followed by a colon. 43 00:02:08,599 --> 00:02:12,510 Finally, to write the body of the function we add a new line, 44 00:02:12,510 --> 00:02:16,900 indent four spaces, and print out the title of our favorite movie. 45 00:02:21,024 --> 00:02:23,962 Don't forget to add quotes around the movie title to indicate that 46 00:02:23,962 --> 00:02:24,700 it's a string. 47 00:02:25,780 --> 00:02:28,753 Awesome work, now you know the basic structure for writing a function. 48 00:02:28,753 --> 00:02:32,783 You have a new tool in your tool belt that is super critical to writing Python and 49 00:02:32,783 --> 00:02:34,630 computer programming in general. 50 00:02:35,630 --> 00:02:37,720 The next step is learning to use your function. 51 00:02:37,720 --> 00:02:42,445 In other words, calling it so that the code inside actually gets executed. 52 00:02:42,445 --> 00:02:44,480 Join me in the next video to learn how.