1 00:00:00,000 --> 00:00:03,402 [AUDIO LOGO] 2 00:00:03,402 --> 00:00:05,607 3 00:00:05,607 --> 00:00:08,440 SPEAKER: So in a moment, we're going to write a very simple program. 4 00:00:08,440 --> 00:00:12,160 And while we do, we're going to think about what a program actually 5 00:00:12,160 --> 00:00:14,440 is at the end of the day. 6 00:00:14,440 --> 00:00:17,710 So we've seen so far that we have this language called Python. 7 00:00:17,710 --> 00:00:20,740 And we can use Python to write programs. 8 00:00:20,740 --> 00:00:23,470 And Python itself has all these tools at our disposal 9 00:00:23,470 --> 00:00:25,540 to actually write those programs with. 10 00:00:25,540 --> 00:00:29,260 So among them are things like conditionals, variables, 11 00:00:29,260 --> 00:00:32,530 loops, and in this case, importantly, functions, 12 00:00:32,530 --> 00:00:35,620 where a function is some piece of code that takes an input 13 00:00:35,620 --> 00:00:37,330 and produces some output. 14 00:00:37,330 --> 00:00:39,850 You can think of it like a verb that you can actually 15 00:00:39,850 --> 00:00:42,670 run inside of your own program. 16 00:00:42,670 --> 00:00:46,750 So here, I'll go ahead and make a program called hello.py 17 00:00:46,750 --> 00:00:48,430 by typing code hello dot py. 18 00:00:48,430 --> 00:00:49,390 And I'll hit Enter. 19 00:00:49,390 --> 00:00:51,490 And I'll get this blank Python file. 20 00:00:51,490 --> 00:00:53,500 I know because it ends with .py here. 21 00:00:53,500 --> 00:00:58,240 And I want to call, or use, my very first function. 22 00:00:58,240 --> 00:01:02,740 And one function built into Python is called the print function, 23 00:01:02,740 --> 00:01:06,110 where the print function takes some text as input 24 00:01:06,110 --> 00:01:10,400 and produces as output this text in my terminal down below. 25 00:01:10,400 --> 00:01:12,350 So I'll type print here. 26 00:01:12,350 --> 00:01:14,990 And I'll follow it with parentheses, just like this. 27 00:01:14,990 --> 00:01:18,020 And I'll type "Hello, world!" 28 00:01:18,020 --> 00:01:19,040 like that. 29 00:01:19,040 --> 00:01:23,630 So now I've used, or called, my very first function, print. 30 00:01:23,630 --> 00:01:26,180 And maybe afterwards, I'll also type print. 31 00:01:26,180 --> 00:01:30,140 Maybe this is CS50P, like this. 32 00:01:30,140 --> 00:01:31,850 So now I have a very simple program. 33 00:01:31,850 --> 00:01:35,000 And I've, in this case, used two functions. 34 00:01:35,000 --> 00:01:38,840 Now, if you're new, you might be asking, how do I know that these are functions? 35 00:01:38,840 --> 00:01:40,715 Well, it just so happens that functions have 36 00:01:40,715 --> 00:01:43,340 these identifiers you can use to determine when you're actually 37 00:01:43,340 --> 00:01:44,930 looking at a function. 38 00:01:44,930 --> 00:01:47,810 Now, one of these identifiers are these parentheses here. 39 00:01:47,810 --> 00:01:51,200 Notice how I called the name of the function, print. 40 00:01:51,200 --> 00:01:54,890 And I followed it with parentheses, open and closed here. 41 00:01:54,890 --> 00:01:57,950 So when you use, or call, functions, you're 42 00:01:57,950 --> 00:02:00,350 often going to type the name of the function followed 43 00:02:00,350 --> 00:02:03,590 by parentheses, and then some input. 44 00:02:03,590 --> 00:02:06,110 Some functions might not even take any input. 45 00:02:06,110 --> 00:02:10,190 You can call them a bit like this-- print, open and closed parentheses. 46 00:02:10,190 --> 00:02:11,930 And that would mean this function doesn't 47 00:02:11,930 --> 00:02:16,070 take any input or, in this case, I haven't given it any input on line 3. 48 00:02:16,070 --> 00:02:19,340 But I have, of course, on lines 1 and 2. 49 00:02:19,340 --> 00:02:22,730 So to give you an idea of what these functions will do, let me go ahead 50 00:02:22,730 --> 00:02:23,570 and run the program. 51 00:02:23,570 --> 00:02:30,380 I'll type python hello.py down below to run this program top to bottom 52 00:02:30,380 --> 00:02:34,790 so that as-- first, we'll see the print function will run on line 1. 53 00:02:34,790 --> 00:02:38,600 And then on line 2, the second print function will run. 54 00:02:38,600 --> 00:02:39,890 So I'll hit Enter here. 55 00:02:39,890 --> 00:02:41,900 And I should see "Hello, world! 56 00:02:41,900 --> 00:02:46,340 This is CS50P" from top to bottom. 57 00:02:46,340 --> 00:02:48,770 So we've used these functions. 58 00:02:48,770 --> 00:02:53,210 But as a programmer, you might also want to create your own functions. 59 00:02:53,210 --> 00:02:57,020 And in particular, you might want to write programs that 60 00:02:57,020 --> 00:02:59,480 are composed of individual functions. 61 00:02:59,480 --> 00:03:01,940 Now, to define your own function-- that is, 62 00:03:01,940 --> 00:03:05,360 to tell us what code should run when I call or use 63 00:03:05,360 --> 00:03:08,510 the function-- you can use the keyword called def, 64 00:03:08,510 --> 00:03:12,230 D-E-F, where def stands for "define." 65 00:03:12,230 --> 00:03:15,883 So maybe because this is the main function-- 66 00:03:15,883 --> 00:03:18,050 I'm about to write the main function in my program-- 67 00:03:18,050 --> 00:03:21,260 I could just call it main, def main, like this. 68 00:03:21,260 --> 00:03:24,740 And I want to include an open parentheses and a closed parentheses, 69 00:03:24,740 --> 00:03:26,660 followed by a colon. 70 00:03:26,660 --> 00:03:29,960 And that then says I have a new function that I've 71 00:03:29,960 --> 00:03:35,510 defined called main that, in this case, won't take any particular inputs. 72 00:03:35,510 --> 00:03:38,360 But now if I want to tell Python or my program 73 00:03:38,360 --> 00:03:43,280 in particular what code should be run when I use this function, 74 00:03:43,280 --> 00:03:47,750 I need to indent those lines of code and put it inside this definition here. 75 00:03:47,750 --> 00:03:50,510 So why don't I take these two, these two prints, 76 00:03:50,510 --> 00:03:53,060 and put it inside of my function called main? 77 00:03:53,060 --> 00:03:56,240 I'll indent both of them and move this back up to the top. 78 00:03:56,240 --> 00:03:58,580 And now I have defined, again, a function 79 00:03:58,580 --> 00:04:01,430 called main that doesn't take any particular input. 80 00:04:01,430 --> 00:04:05,780 But it does run these two functions-- first the print on line 2, 81 00:04:05,780 --> 00:04:09,230 and then the print on line 3. 82 00:04:09,230 --> 00:04:12,830 So now that you've seen how to define a function, 83 00:04:12,830 --> 00:04:17,450 you could think about how or who defined the print function for us 84 00:04:17,450 --> 00:04:20,450 because right now, we're saying print "Hello, world!" 85 00:04:20,450 --> 00:04:22,370 But what does it mean to print? 86 00:04:22,370 --> 00:04:24,530 What goes into printing something to the terminal? 87 00:04:24,530 --> 00:04:26,870 Well, it just so happens that the Python developers 88 00:04:26,870 --> 00:04:29,905 defined what happens in the print function 89 00:04:29,905 --> 00:04:32,030 elsewhere in the Python library, which you'll learn 90 00:04:32,030 --> 00:04:33,830 more about as we go through the course. 91 00:04:33,830 --> 00:04:38,060 But for now, you can just know that you can use this function, print. 92 00:04:38,060 --> 00:04:41,390 But somewhere else, somebody before you has defined it and allowed 93 00:04:41,390 --> 00:04:45,320 you to use it in this context, just like I've now defined a function 94 00:04:45,320 --> 00:04:47,240 called main here. 95 00:04:47,240 --> 00:04:49,940 So let me go and run this program now. 96 00:04:49,940 --> 00:04:52,550 I'll say python hello.py. 97 00:04:52,550 --> 00:04:54,670 Hit Enter. 98 00:04:54,670 --> 00:04:58,090 I don't see anything, which is maybe surprising 99 00:04:58,090 --> 00:05:03,190 if you're new because here, I said I'm going to define a function called main. 100 00:05:03,190 --> 00:05:05,470 And within that function, I want to do the following. 101 00:05:05,470 --> 00:05:10,450 I want to print "Hello, world!" and then print, "This is CS50P." 102 00:05:10,450 --> 00:05:15,580 But in Python, defining a function is different from running it, 103 00:05:15,580 --> 00:05:17,830 or this technical term called calling it. 104 00:05:17,830 --> 00:05:20,620 So I've defined my function called main. 105 00:05:20,620 --> 00:05:22,600 But I haven't called main. 106 00:05:22,600 --> 00:05:28,180 And by convention, I can do that using two new lines underneath my definition 107 00:05:28,180 --> 00:05:31,210 here and then calling main. 108 00:05:31,210 --> 00:05:35,800 So I'm going to now run whatever I've defined as part of my main function 109 00:05:35,800 --> 00:05:36,340 here. 110 00:05:36,340 --> 00:05:39,970 I'll type python hello.py again, hit Enter, 111 00:05:39,970 --> 00:05:42,250 and now I should see "Hello, world! 112 00:05:42,250 --> 00:05:44,710 This is CS50P." 113 00:05:44,710 --> 00:05:47,350 Because this program is read top to bottom, 114 00:05:47,350 --> 00:05:49,540 it will first define a function called main. 115 00:05:49,540 --> 00:05:53,620 It will then say what functions compose that function, which functions should 116 00:05:53,620 --> 00:05:55,520 be run when I call this function. 117 00:05:55,520 --> 00:06:00,530 And then down below, I finally actually call it on line 6. 118 00:06:00,530 --> 00:06:05,630 And so now in this case, you've seen that my entire program is really 119 00:06:05,630 --> 00:06:06,200 a function. 120 00:06:06,200 --> 00:06:09,050 As you go off and build new and better programs, 121 00:06:09,050 --> 00:06:10,820 you'll see that a lot of what you're doing 122 00:06:10,820 --> 00:06:14,150 is just writing things that take inputs and produce outputs. 123 00:06:14,150 --> 00:06:17,770 And those things are called functions. 124 00:06:17,770 --> 00:06:19,000