1 00:00:00,000 --> 00:00:00,160 2 00:00:00,160 --> 00:00:04,010 >> SPEAKER 1: So I feel like I tend to write printf hello world or printf 3 00:00:04,010 --> 00:00:08,160 hello percent s quite often in contexts where I want to print out a 4 00:00:08,160 --> 00:00:11,360 string, sometimes asking the user part of that string. 5 00:00:11,360 --> 00:00:14,980 Well, wouldn't it be nice if we created a function of our own called, 6 00:00:14,980 --> 00:00:19,900 say, print name, whose purpose in life is simply to print hello comma 7 00:00:19,900 --> 00:00:24,560 so-and-so by accepting the so-and-so as an argument to that function? 8 00:00:24,560 --> 00:00:29,220 We can declare a function of our own just as we can in Scratch as follows. 9 00:00:29,220 --> 00:00:35,280 >> In other words, suppose I'd like to printf your name and then ask the user 10 00:00:35,280 --> 00:00:39,470 for his or her name with string s equals GetString. 11 00:00:39,470 --> 00:00:42,060 And then I want to call a function called print name 12 00:00:42,060 --> 00:00:44,340 passing an s as its input. 13 00:00:44,340 --> 00:00:47,770 >> Now unfortunately, there is no function called print name in C or in 14 00:00:47,770 --> 00:00:48,950 the cs50 library. 15 00:00:48,950 --> 00:00:51,220 But no matter, because we can make it ourselves. 16 00:00:51,220 --> 00:00:54,560 To declare a function of our own and then implement it, we can simply make 17 00:00:54,560 --> 00:00:59,000 some room in this file and declare this function as follows, void-- 18 00:00:59,000 --> 00:01:01,900 signifying that the function's not actually going to return something, 19 00:01:01,900 --> 00:01:03,890 even though it will have a side effect-- 20 00:01:03,890 --> 00:01:08,030 print name, and then let's specify in parentheses that print name expects an 21 00:01:08,030 --> 00:01:09,680 argument of type string. 22 00:01:09,680 --> 00:01:12,180 And in general, we'll think of that as a name. 23 00:01:12,180 --> 00:01:15,140 >> And let's open our brace and close our brace. 24 00:01:15,140 --> 00:01:21,930 And then printf inside hello comma percent s new line comma name. 25 00:01:21,930 --> 00:01:24,570 In other words, even though this function's only one line of code, it 26 00:01:24,570 --> 00:01:27,290 does now have some higher level purpose to print 27 00:01:27,290 --> 00:01:28,950 someone's name with a greeting. 28 00:01:28,950 --> 00:01:33,115 >> Now inside of main, notice that I'm calling print name by passing an s. 29 00:01:33,115 --> 00:01:35,830 And so I seem now to have a complete program. 30 00:01:35,830 --> 00:01:38,650 The only difference being from others we've written is that not only does 31 00:01:38,650 --> 00:01:42,480 this program have main, it also has a declaration and implementation of 32 00:01:42,480 --> 00:01:44,740 print name, a function of my own. 33 00:01:44,740 --> 00:01:49,505 >> Let's now compile my program with make function zero, and run it with dot 34 00:01:49,505 --> 00:01:51,080 slash function zero. 35 00:01:51,080 --> 00:01:53,390 When prompted for my name I'll type in David. 36 00:01:53,390 --> 00:01:55,830 And I indeed am greeted with "Hello, David." 37 00:01:55,830 --> 00:02:00,160 >> Now, it turns out I very deliberately declared print name above main, 38 00:02:00,160 --> 00:02:03,860 thereby telling C in advance that there exists a function called print 39 00:02:03,860 --> 00:02:06,930 name so that I could use it later in main. 40 00:02:06,930 --> 00:02:11,610 Now, had I instead put print name at the bottom of my file, as frankly I 41 00:02:11,610 --> 00:02:15,310 probably should do so that main remains the first thing that I or 42 00:02:15,310 --> 00:02:18,450 another user sees in my code, it turns out that my code is 43 00:02:18,450 --> 00:02:19,780 not going to compile. 44 00:02:19,780 --> 00:02:23,610 >> Specifically, if I go back to my terminal window and run make function 45 00:02:23,610 --> 00:02:26,120 zero now, notice that I'm yelled at. 46 00:02:26,120 --> 00:02:30,340 If I scroll up in my terminal window, I'll see this first error in red, 47 00:02:30,340 --> 00:02:33,120 implicit declaration of function print name. 48 00:02:33,120 --> 00:02:34,220 >> Now, what does that mean? 49 00:02:34,220 --> 00:02:37,190 Well, even though I've implemented print name in this file, I've not 50 00:02:37,190 --> 00:02:40,080 implemented it before I've used it in main. 51 00:02:40,080 --> 00:02:43,160 Now, we could go back to the old version and put print name's 52 00:02:43,160 --> 00:02:44,450 implementation up top. 53 00:02:44,450 --> 00:02:48,730 >> But I can instead just give C a hint that there's a function to come. 54 00:02:48,730 --> 00:02:55,390 Specifically, above main I can declare print name just as I did earlier, but 55 00:02:55,390 --> 00:02:59,450 end the line with a semicolon, thereby promising to implement but not yet 56 00:02:59,450 --> 00:03:02,180 actually implementing the function called print name. 57 00:03:02,180 --> 00:03:05,860 I can then have main at the top of my file as I hoped, and I can actually 58 00:03:05,860 --> 00:03:07,440 implement print name at the bottom. 59 00:03:07,440 --> 00:03:12,860 >> If I now return to my terminal window and recompile function zero with make, 60 00:03:12,860 --> 00:03:14,390 this time it compiles. 61 00:03:14,390 --> 00:03:18,240 And if I run it with dot slash function zero providing my name, I'm 62 00:03:18,240 --> 00:03:20,650 greeted as "Hello, David." 63 00:03:20,650 --> 00:03:22,268