1 00:00:08,030 --> 00:00:14,400 Welcome back. The basic syntax for defining a function is the word def, 2 00:00:14,400 --> 00:00:17,295 D-E-F, its short for define. 3 00:00:17,295 --> 00:00:25,260 Then you get any Python variable name and then you have an open and close parenthesis. 4 00:00:25,260 --> 00:00:28,920 Inside the parentheses, we're going to see later that you could have 5 00:00:28,920 --> 00:00:34,180 more variable names but we don't have any for this function and then we've got a colon. 6 00:00:34,900 --> 00:00:39,180 Below that, you've got an indented block of code. 7 00:00:39,180 --> 00:00:44,330 You've already seen indented blocks of code with the while loop, the for loop, 8 00:00:44,330 --> 00:00:49,995 or if statements, they work the same way for function definitions. 9 00:00:49,995 --> 00:00:53,360 All the lines that are indented by that same number of spaces they're 10 00:00:53,360 --> 00:00:57,185 all part of the function definition for the hello function. 11 00:00:57,185 --> 00:01:00,830 And when we get another line of code down here on line five, 12 00:01:00,830 --> 00:01:03,085 that's outdented at the same level as def, 13 00:01:03,085 --> 00:01:07,020 that's going to be the end of the function definition. 14 00:01:07,020 --> 00:01:14,415 We have an optional comment on the first line of the function. 15 00:01:14,415 --> 00:01:19,370 If it's included it's called a doc string and there are some tools in Python for 16 00:01:19,370 --> 00:01:21,290 automatically generating documentation of 17 00:01:21,290 --> 00:01:25,265 a program that'll show the docstrings that are associated with functions. 18 00:01:25,265 --> 00:01:27,470 It's often a multi-line string, 19 00:01:27,470 --> 00:01:30,290 that's why we use this triple quotes. 20 00:01:30,290 --> 00:01:35,160 We could have some more docstring here. 21 00:01:35,160 --> 00:01:41,010 The triple quotes lets you have a string that goes onto multiple lines. 22 00:01:41,650 --> 00:01:47,075 Now when we execute just the code that we have here, 23 00:01:47,075 --> 00:01:50,195 nothing is going to print out. 24 00:01:50,195 --> 00:01:53,705 Even though there are two print statements on lines four and five, 25 00:01:53,705 --> 00:01:55,810 they won't actually be executed. 26 00:01:55,810 --> 00:02:01,175 All that happens from lines one through five is that the function object gets created. 27 00:02:01,175 --> 00:02:05,100 That function doesn't get executed. 28 00:02:05,560 --> 00:02:13,780 Lines four and five here are only going to execute if we invoke the function. 29 00:02:14,870 --> 00:02:19,340 So, I'm going to have some code that's outdented at the same level as 30 00:02:19,340 --> 00:02:23,860 the def and I'm going to invoke the function hello. 31 00:02:23,860 --> 00:02:30,000 Now, when we execut it lines four and five will run. 32 00:02:30,000 --> 00:02:34,060 And it says, hello glad to meet you. 33 00:02:37,520 --> 00:02:40,740 Suppose I print something that says, 34 00:02:40,740 --> 00:02:45,320 we are here and then another invocation of hello. 35 00:02:45,320 --> 00:02:49,740 See if you can predict what we're going to get in the output window. 36 00:02:54,220 --> 00:02:57,950 What we get is 37 00:02:57,950 --> 00:03:05,450 the first two lines come from our first invocation of hello and then we get, 38 00:03:05,450 --> 00:03:08,360 the we are here, and then we get 39 00:03:08,360 --> 00:03:13,440 two more lines that come from the second invocation of hello. 40 00:03:14,630 --> 00:03:17,640 Let's see that in codelens. 41 00:03:17,640 --> 00:03:23,980 So, you can see that executing lines one through five just creates 42 00:03:23,980 --> 00:03:32,310 a variable called hello whose value is a function object. 43 00:03:32,310 --> 00:03:35,580 It doesn't execute that function object. 44 00:03:36,800 --> 00:03:43,790 When I invoke the function on line seven it passes control to the function. 45 00:03:43,790 --> 00:03:48,185 So, we then actually execute the contents that are inside of it. 46 00:03:48,185 --> 00:03:53,920 So, we execute line four which prints out, hello, 47 00:03:53,920 --> 00:03:57,935 we execute line five and now we're done so we're going to 48 00:03:57,935 --> 00:04:04,860 resume back here after the execution of hello. 49 00:04:06,100 --> 00:04:10,025 So, we get to line eight and we print, we are here, 50 00:04:10,025 --> 00:04:14,330 and then we have another invocation that passes control to the function, 51 00:04:14,330 --> 00:04:19,040 it does its stuff and then it passes control back after line nine, 52 00:04:19,040 --> 00:04:22,320 and we're at the end of the function execution. 53 00:04:23,680 --> 00:04:27,290 So, that's the basics of function definition. 54 00:04:27,290 --> 00:04:30,870 The syntax, we start with the word def, 55 00:04:32,780 --> 00:04:37,290 and then we have a function name, and then parentheses, 56 00:04:37,290 --> 00:04:42,485 and then a colon, we've got an indented block of code for the contents of the function. 57 00:04:42,485 --> 00:04:45,695 Executing the def statement, 58 00:04:45,695 --> 00:04:53,550 that just creates the function it doesn't execute it and we need other code afterwards, 59 00:04:53,550 --> 00:04:55,260 like we have on line seven. 60 00:04:55,260 --> 00:04:59,720 When we do a function invocation it passes control to the code for the function. 61 00:04:59,720 --> 00:05:03,560 So, those lines of code inside the function can execute it and then we 62 00:05:03,560 --> 00:05:08,780 resume right after the spot where the function invocation happens. 63 00:05:08,780 --> 00:05:13,110 See you next time when we add formal parameters.