1 00:00:00,506 --> 00:00:10,776 [ Silence ] 2 00:00:11,276 --> 00:00:14,916 >> This is CS50, the end of Week 2. 3 00:00:15,006 --> 00:00:17,486 So I thought I'd begin with a short note that one 4 00:00:17,486 --> 00:00:19,546 of your classmates sent to me earlier this week. 5 00:00:19,546 --> 00:00:21,486 I'll leave it anonymous but read the content. 6 00:00:21,786 --> 00:00:24,076 Your lecture -- I don't normally do this with your emails -- 7 00:00:24,366 --> 00:00:26,806 but your lecture the other day reminded me of this scene 8 00:00:26,806 --> 00:00:28,766 from The Computer Wore Tennis Shoes. 9 00:00:28,836 --> 00:00:29,676 If you've never seen it, 10 00:00:29,676 --> 00:00:31,716 I'd highly recommend it just for the technology. 11 00:00:31,986 --> 00:00:34,286 Obviously, it is a little old, but it is funny 12 00:00:34,286 --> 00:00:37,596 to see what people thought about computers in 1969. 13 00:00:38,496 --> 00:00:43,496 So I give you a scene from The Computer Wore Tennis Shoes. 14 00:00:44,106 --> 00:00:48,476 >> [Recorded Demonstration] Computer our input, 15 00:00:48,686 --> 00:00:51,556 memory control, arithmetic, and logic and output. 16 00:00:52,246 --> 00:00:54,436 I think we can see demonstrative from these charts, 17 00:00:54,436 --> 00:00:56,606 that man has done a rather admirable job 18 00:00:56,606 --> 00:00:58,056 of imitating the human brain. 19 00:00:59,086 --> 00:01:01,896 And even though it's an imitation, in many ways, 20 00:01:01,896 --> 00:01:03,746 the machine that we've developed can operate more 21 00:01:03,746 --> 00:01:04,806 than we can ourselves. 22 00:01:05,696 --> 00:01:08,246 And now in the past couple of weeks, I've been working 23 00:01:08,246 --> 00:01:11,406 on a small experiment, which is intended 24 00:01:11,406 --> 00:01:13,756 to show how we can be replaced by a computer. 25 00:01:15,156 --> 00:01:17,306 >> Hey, this may be a way of getting rid of Dean Higgins. 26 00:01:19,176 --> 00:01:20,376 >> We live in hope Dexter. 27 00:01:23,496 --> 00:01:26,146 And now let's suppose that your parents are going away 28 00:01:26,146 --> 00:01:28,326 for the weekend and they leave you 29 00:01:28,326 --> 00:01:29,766 with the following instructions: 30 00:01:30,096 --> 00:01:32,746 If it's raining tomorrow morning, when you wake up, 31 00:01:33,216 --> 00:01:35,536 you are to close the windows, open the door 32 00:01:35,536 --> 00:01:38,346 and let the cat in, phone the grocer and have him send 33 00:01:38,346 --> 00:01:40,096 over the following list of groceries. 34 00:01:40,466 --> 00:01:40,786 All right. 35 00:01:41,786 --> 00:01:43,836 Well, you wake up in the morning and it is raining 36 00:01:43,836 --> 00:01:46,256 and being the conscientious and reliable young people 37 00:01:46,256 --> 00:01:49,626 that I know you to be, you probably forget to do it. 38 00:01:50,466 --> 00:01:52,596 Well, actually, I'm sure you would do it. 39 00:01:52,966 --> 00:01:57,696 But let's see if a computer could also do it. 40 00:01:57,836 --> 00:02:02,266 Now this is a rain gauge, and it's sitting on a roof 41 00:02:02,266 --> 00:02:06,386 of a building, and when the water falls into this gauge, 42 00:02:06,466 --> 00:02:09,166 it sends a signal to the logic unit of the computer, 43 00:02:09,496 --> 00:02:12,556 which immediately notifies the memory bank of the computer 44 00:02:12,556 --> 00:02:14,656 to seek out its instructions for a rainy day. 45 00:02:15,076 --> 00:02:16,576 The memory bank, in turn, 46 00:02:16,676 --> 00:02:20,466 activates the electronic circuits; the window closes, 47 00:02:20,626 --> 00:02:23,776 the door opens, hopefully the cat comes in, 48 00:02:24,266 --> 00:02:27,496 and the automatic telephone goes into action; right? 49 00:02:28,606 --> 00:02:28,976 Well, let's see. 50 00:02:29,516 --> 00:02:33,616 [ Electronic Noises ] 51 00:02:34,116 --> 00:02:36,436 >> The rain falls. 52 00:02:37,516 --> 00:02:48,886 [ Electronic Noises ] 53 00:02:49,386 --> 00:02:50,446 >> This is a recording. 54 00:02:50,446 --> 00:02:57,436 Please deliver the following groceries to 445 Attic Street: 55 00:02:57,436 --> 00:03:00,346 1 pound of bacon, 2 dozen eggs, 4 quarts of milk. 56 00:03:00,376 --> 00:03:00,976 >> Well, that's enough of that. 57 00:03:01,516 --> 00:03:06,126 [ Applause ] 58 00:03:06,626 --> 00:03:06,996 >> Thank you. 59 00:03:07,176 --> 00:03:07,546 Thank you. 60 00:03:08,096 --> 00:03:09,966 Well, so much for the speed of the computer. 61 00:03:10,186 --> 00:03:12,526 Now another very important feature is its 62 00:03:12,526 --> 00:03:13,636 infallible memory. 63 00:03:14,506 --> 00:03:17,896 Before, this computer was owned by the AJ Arnold Company. 64 00:03:17,926 --> 00:03:20,436 It used to be employed by the Space Research Center 65 00:03:20,436 --> 00:03:22,176 at the specific Institute of Technology. 66 00:03:22,176 --> 00:03:25,136 And they were kind enough to send us this magnetic tank 67 00:03:25,646 --> 00:03:29,596 which we'll call forth from the computer solutions to a series 68 00:03:29,596 --> 00:03:31,046 of problems that would occur 69 00:03:31,046 --> 00:03:33,786 on a simulated flight to the planet Saturn. 70 00:03:34,056 --> 00:03:36,536 Now, mind you, it was 20 years ago 71 00:03:36,536 --> 00:03:39,806 that this program was operational, 20 years. 72 00:03:41,256 --> 00:03:45,766 Well, if you'll watch the center panel, please. 73 00:03:46,516 --> 00:04:04,626 [ Electronic Noises ] 74 00:04:05,126 --> 00:04:06,476 >> Well, it seems like our flight 75 00:04:06,526 --> 00:04:08,136 to Saturn will be a little delayed. 76 00:04:08,746 --> 00:04:12,546 >> In any case, that high school has like $10 million worth 77 00:04:12,546 --> 00:04:14,376 of computer equipment apparently in the front of it. 78 00:04:14,486 --> 00:04:17,246 Computers don't have so much the same kind of blinking lights. 79 00:04:17,246 --> 00:04:18,576 There's an allusion there to memory, 80 00:04:18,576 --> 00:04:20,616 which will be a little apropos for Problem Set 2. 81 00:04:20,926 --> 00:04:23,726 For Problem Set 1, as you may have seen already, 82 00:04:23,726 --> 00:04:26,326 what I thought I'd -- I had not, in fact, seen that clip; 83 00:04:26,506 --> 00:04:30,426 so I did a quick Googling and that's actually as relevant 84 00:04:30,426 --> 00:04:32,616 as the movie really gets I think to computing. 85 00:04:32,616 --> 00:04:34,696 If you read a quick plot summary here, 86 00:04:34,876 --> 00:04:36,156 I'll just give you a little teaser. 87 00:04:36,156 --> 00:04:36,356 All right. 88 00:04:36,356 --> 00:04:37,776 This is more of a spoiler alert, 89 00:04:38,036 --> 00:04:40,136 but you've had 41 years to see this movie. 90 00:04:40,196 --> 00:04:43,656 So Kurt Russell and his friends attend a small private college, 91 00:04:43,656 --> 00:04:45,346 which cannot afford to buy a computer. 92 00:04:45,416 --> 00:04:47,556 The students persuade wealthy businessman 93 00:04:47,556 --> 00:04:49,576 to donate an old computer to the college. 94 00:04:49,946 --> 00:04:52,766 He is the secret head of a large illegal gambling ring 95 00:04:53,046 --> 00:04:55,466 which use the computer for its operations. 96 00:04:55,666 --> 00:04:58,086 While installing a replacement part during a thunderstorm, 97 00:04:58,086 --> 00:04:59,966 Riley, the Professor, I presume, is zapped 98 00:05:00,156 --> 00:05:01,696 and becomes a human computer. 99 00:05:01,976 --> 00:05:04,336 He now has super-human mathematical talent, can read 100 00:05:04,336 --> 00:05:06,386 and remember the contents of an encyclopedia volume 101 00:05:06,386 --> 00:05:08,706 in a few minutes, and speak a language fluently 102 00:05:08,706 --> 00:05:09,956 after reading one textbook. 103 00:05:10,246 --> 00:05:13,486 Riley, single-handedly, leads Medfield's team in victories 104 00:05:13,486 --> 00:05:14,636 against other colleges. 105 00:05:14,636 --> 00:05:16,256 During the tournament, a trigger word, 106 00:05:16,256 --> 00:05:19,996 that causes Riley unknowingly to recite on television the details 107 00:05:19,996 --> 00:05:21,486 of Arno's gambling ring. 108 00:05:21,776 --> 00:05:23,576 Arno's henchman just goes on. 109 00:05:23,726 --> 00:05:28,556 So an interesting movie but a fun clip, perhaps, nonetheless. 110 00:05:28,556 --> 00:05:34,166 So a few announcements and those are: one, sectioning. 111 00:05:34,466 --> 00:05:36,816 We've had super sections, as we called them this past week, 112 00:05:36,816 --> 00:05:39,486 really to -- available to anyone who would like to attend. 113 00:05:39,486 --> 00:05:41,566 This coming week, we'll start a more traditional 114 00:05:41,676 --> 00:05:42,436 assigned sections. 115 00:05:42,466 --> 00:05:43,386 The sectioning tool, 116 00:05:43,386 --> 00:05:46,276 FAAS Sectioning Tool will be available to you later today. 117 00:05:46,306 --> 00:05:48,336 We're just waiting for confirmation on some time. 118 00:05:48,586 --> 00:05:50,856 We'll post an announcement to the course's homepage 119 00:05:50,856 --> 00:05:53,116 at CS50.net on the top with a link. 120 00:05:53,166 --> 00:05:54,856 So if you want to check that out after dinner time or so, 121 00:05:54,856 --> 00:05:55,766 you can fill that out. 122 00:05:56,036 --> 00:05:58,626 And we will close sectioning by this Friday at the time 123 00:05:58,626 --> 00:06:00,416 to be stated on the course's website. 124 00:06:00,676 --> 00:06:02,636 Inevitably, in a class of this size, we're going to have 125 00:06:02,636 --> 00:06:04,826 to make some adjustments; so you'll receive instructions 126 00:06:04,826 --> 00:06:06,916 when you get your section assignment on how to deal 127 00:06:06,916 --> 00:06:08,586 with unforeseen conflicts if, for instance, 128 00:06:08,586 --> 00:06:10,546 some other class just moved you around. 129 00:06:11,096 --> 00:06:13,176 If you have been working and are still working 130 00:06:13,176 --> 00:06:16,456 on problem Set One, do realize that a good deal of support 131 00:06:16,456 --> 00:06:17,806 and assistance is available to you. 132 00:06:17,806 --> 00:06:20,336 So this is our little Google calendar that we embed 133 00:06:20,366 --> 00:06:22,966 in the course's website, and this is the number 134 00:06:22,966 --> 00:06:25,816 of office hours we had just in the next couple of days alone. 135 00:06:25,816 --> 00:06:27,966 If you look at this in graphical format, I mean, 136 00:06:27,966 --> 00:06:30,146 it's actually pretty remarkable just how many 137 00:06:30,146 --> 00:06:31,136 of us will be down there. 138 00:06:31,136 --> 00:06:33,546 So it's in Science Center B14, which is, 139 00:06:33,546 --> 00:06:35,566 you go into the Science Center, you go down the steps, 140 00:06:35,566 --> 00:06:38,156 turn left, and there's a pretty big computer lab there. 141 00:06:38,396 --> 00:06:40,356 The way the workflow generally works is you just grab a 142 00:06:40,356 --> 00:06:41,736 computer or bring your laptop. 143 00:06:41,736 --> 00:06:44,446 We use the whiteboards alongside one of the walls, 144 00:06:44,646 --> 00:06:46,696 where you just write down your name if you have a question. 145 00:06:46,696 --> 00:06:48,776 We just go through the list top to bottom, left to right, 146 00:06:48,776 --> 00:06:50,766 and cross names off as we go. 147 00:06:50,986 --> 00:06:54,316 Realize that there's an inevitably challenge for us 148 00:06:54,316 --> 00:06:58,336 and actually helping people in this kind of context, 149 00:06:58,336 --> 00:07:00,146 especially as the deadline really approaches. 150 00:07:00,436 --> 00:07:02,436 So do view these particular office hours. 151 00:07:02,486 --> 00:07:04,946 It's definitely an opportunity for help, but particularly, 152 00:07:04,946 --> 00:07:07,856 as an opportunity to work with someone for just a few moments 153 00:07:08,096 --> 00:07:09,976 to help get you unstuck 154 00:07:09,976 --> 00:07:12,696 or to get you past some stumbling block that, otherwise, 155 00:07:12,696 --> 00:07:14,606 if you struggled with on your own, you might sink 156 00:07:14,606 --> 00:07:17,066 or waste even multiple hours on just something 157 00:07:17,066 --> 00:07:18,076 that you're not seeing. 158 00:07:18,076 --> 00:07:20,726 So if you have more conceptual oriented questions, 159 00:07:20,986 --> 00:07:22,636 you can absolutely bring them to office hours, 160 00:07:22,666 --> 00:07:24,766 but just realize there's always a crunch time wise, 161 00:07:24,766 --> 00:07:26,186 especially on Wednesday's and Thursday's. 162 00:07:26,456 --> 00:07:28,866 So do reach out ultimately to your assigned teaching fellow, 163 00:07:28,866 --> 00:07:32,046 to me, to the help list, to the forums online. 164 00:07:32,236 --> 00:07:34,466 If you'd like to sit down with one of us more intimately 165 00:07:34,466 --> 00:07:37,576 and more sort of tranquilly, and work through problems for more 166 00:07:37,576 --> 00:07:39,106 than just a couple minutes at a time. 167 00:07:39,426 --> 00:07:40,606 So with that said, this -- 168 00:07:40,636 --> 00:07:44,396 these forums, I'm referring to are these at help.cs50.net. 169 00:07:44,896 --> 00:07:47,316 Our bulletin boards of sorts, discussion forums, 170 00:07:47,516 --> 00:07:49,806 where you can not only browse discussions, 171 00:07:49,806 --> 00:07:51,376 questions that your classmates have asked, 172 00:07:51,576 --> 00:07:53,696 you can certainly ask questions of your own. 173 00:07:53,696 --> 00:07:55,266 If you would like to flag them as private, 174 00:07:55,266 --> 00:07:57,376 because you just don't want your classmates 175 00:07:57,406 --> 00:07:59,616 to see what you're asking, you can certainly do that. 176 00:07:59,616 --> 00:08:01,726 And if you're sharing a particularly decent amount 177 00:08:01,726 --> 00:08:03,906 of code, you should definitely submit it as private, 178 00:08:04,216 --> 00:08:07,026 or if easier, you can email help at cs50.net. 179 00:08:07,026 --> 00:08:11,086 And realize too, as we say on this home page here, by default, 180 00:08:11,086 --> 00:08:15,116 we anonomize you when you log into this bulletin board, 181 00:08:15,116 --> 00:08:17,826 whereby, you're all logged in as quote unquote students. 182 00:08:18,036 --> 00:08:19,876 The staff can still figure out who you are in order 183 00:08:19,876 --> 00:08:22,046 to help you most effectively but as for your classmates, 184 00:08:22,196 --> 00:08:24,116 they'll just see you and your questions as student. 185 00:08:24,116 --> 00:08:25,666 And this is very much deliberate on our part 186 00:08:25,916 --> 00:08:29,576 so you just don't feel that inevitable angst when it comes 187 00:08:29,576 --> 00:08:31,536 to asking what you think is a dumb question 188 00:08:31,536 --> 00:08:33,826 and probably isn't, but you don't need 189 00:08:33,826 --> 00:08:34,966 to disclose your name. 190 00:08:34,966 --> 00:08:38,536 And if you do wish to disclose your name, you certainly can, 191 00:08:39,086 --> 00:08:40,456 per the directions on this page. 192 00:08:40,456 --> 00:08:44,086 So with that said, let me go ahead 193 00:08:44,086 --> 00:08:46,916 and mention one other thing. 194 00:08:46,916 --> 00:08:49,926 So one of the other features available is a link up top 195 00:08:49,926 --> 00:08:52,606 on the course's office hours page that will lead you 196 00:08:52,606 --> 00:08:54,646 to what we call the Vrtual Terminal Room. 197 00:08:54,936 --> 00:08:58,536 The Virtual Terminal Room is really a glorified chat room, 198 00:08:58,826 --> 00:09:01,836 but it's a piece of software that you can log into, 199 00:09:01,836 --> 00:09:03,986 via the office hours page, as yourself. 200 00:09:03,986 --> 00:09:06,356 After logging into the course's website, you'll appear 201 00:09:06,356 --> 00:09:10,616 on the top left-hand corner as I just did with your actual names 202 00:09:10,616 --> 00:09:12,526 that we know exactly who we're working with 203 00:09:12,806 --> 00:09:16,006 and then what you can do is raise your hand sort to speak. 204 00:09:16,046 --> 00:09:18,776 There's a little icon of a hand here, at top left, you go ahead 205 00:09:18,776 --> 00:09:21,326 and click that, and what happens is, we, the staff, 206 00:09:21,626 --> 00:09:24,506 see a little blinking icon in the top left that says one 207 00:09:24,506 --> 00:09:27,286 out of two hands, one out of 10 hands, or some number of hands, 208 00:09:27,536 --> 00:09:29,516 and it maintains in the little row 209 00:09:29,516 --> 00:09:31,486 up there your placement in line. 210 00:09:31,556 --> 00:09:33,346 So this is pretty much a computer version 211 00:09:33,346 --> 00:09:34,746 of the whiteboard in the Science Center. 212 00:09:35,036 --> 00:09:37,836 The goal of virtual office hours is if you're sitting 213 00:09:37,836 --> 00:09:41,036 down in mather or the quad or wherever, even off campus 214 00:09:41,036 --> 00:09:43,346 on some weekend, we'll still hold, not as many, 215 00:09:43,346 --> 00:09:44,916 but some virtual office hours. 216 00:09:44,916 --> 00:09:46,766 So you can log into this, not only chat 217 00:09:46,866 --> 00:09:49,226 with this interactively, but on the right-hand side here, 218 00:09:49,376 --> 00:09:50,926 which is just a wallpaper right now, 219 00:09:51,226 --> 00:09:55,406 you can actually grant us view or control of your own screen. 220 00:09:55,406 --> 00:09:57,556 So if you've got putty or terminal up on the screen, 221 00:09:57,556 --> 00:10:00,386 and you're having some trouble that's really best seen with -- 222 00:10:00,926 --> 00:10:04,176 in that natural environment, we can actually see sort 223 00:10:04,176 --> 00:10:05,036 of tech-support style 224 00:10:05,036 --> 00:10:06,706 on the right-hand side what you're doing, 225 00:10:06,706 --> 00:10:07,956 and you can see everything we're doing. 226 00:10:07,956 --> 00:10:10,266 You have to grant us control explicitly. 227 00:10:10,266 --> 00:10:13,606 So do keep this in mind as an available option as well. 228 00:10:14,926 --> 00:10:18,156 Lastly, that was it. 229 00:10:18,366 --> 00:10:22,266 Let me go ahead and -- oh, that's nice, someone learned how 230 00:10:22,266 --> 00:10:23,846 to send right messages on the cloud. 231 00:10:23,846 --> 00:10:24,266 All right. 232 00:10:24,786 --> 00:10:25,866 I'll write you back later. 233 00:10:26,156 --> 00:10:28,146 So that brings us back to -- 234 00:10:29,216 --> 00:10:32,236 it's a feature that will be disabled by tonight, I'm sure. 235 00:10:32,766 --> 00:10:36,186 Well done, though, class. 236 00:10:37,516 --> 00:10:38,956 All right. 237 00:10:38,956 --> 00:10:41,476 Oh, so a couple of -- oh, okay. 238 00:10:41,536 --> 00:10:44,196 So a couple of FAQ's that have come up in section 239 00:10:44,416 --> 00:10:45,916 or rather in office hours. 240 00:10:45,966 --> 00:10:48,386 So we keep track of the types of questions that come up, 241 00:10:48,386 --> 00:10:50,236 unless I said something stupid in class 242 00:10:50,236 --> 00:10:51,406 or been confusing, certainly. 243 00:10:51,656 --> 00:10:53,626 So what I wanted to do is loop back, at least with a couple 244 00:10:53,626 --> 00:10:56,226 of things that we see recurring and very understandably so, 245 00:10:56,226 --> 00:10:57,446 but let's try to clear up a couple 246 00:10:57,446 --> 00:10:59,466 of frequently occurring issues. 247 00:10:59,466 --> 00:11:02,356 So we've seen a lot of this, for instance, in office hours 248 00:11:02,356 --> 00:11:04,496 and maybe you too have done or are currently doing this, 249 00:11:04,826 --> 00:11:06,866 but there's a problem with writing a statement, 250 00:11:06,866 --> 00:11:09,506 like print F, followed by, get int, alone. 251 00:11:09,646 --> 00:11:11,976 So someone who's a little more comfortable with this 252 00:11:11,976 --> 00:11:13,296 at this point, what's the problem here? 253 00:11:13,296 --> 00:11:14,356 Or what is the problem? 254 00:11:14,486 --> 00:11:16,356 >> You don't know where to store it as one. 255 00:11:16,516 --> 00:11:19,616 >> Yes. You don't actually store the return value of, get int. 256 00:11:19,616 --> 00:11:21,896 So we'll talk a bit more about this idea of functions today, 257 00:11:21,896 --> 00:11:24,586 but, get int, again, can think of it as a black box, 258 00:11:24,766 --> 00:11:27,896 whose purpose in life is to prompt the human for input 259 00:11:27,896 --> 00:11:29,126 and pester the human if he 260 00:11:29,126 --> 00:11:32,406 or she doesn't actually give it int, but then once it has that, 261 00:11:32,406 --> 00:11:33,596 has entered it from the user, 262 00:11:33,596 --> 00:11:35,286 it's quote unquote, returns it to you. 263 00:11:35,506 --> 00:11:37,496 But if you don't actually do anything with it, 264 00:11:37,776 --> 00:11:40,346 it simply disappears, and, therefore, it's -- 265 00:11:40,476 --> 00:11:41,836 well done, classmate Number Two. 266 00:11:42,326 --> 00:11:43,896 The beeping means a hello message. 267 00:11:45,076 --> 00:11:45,556 Let's see. 268 00:11:46,196 --> 00:11:49,136 Just to illustrate this point, I thought it would be more fun 269 00:11:49,136 --> 00:11:50,276 to do this without just me. 270 00:11:50,276 --> 00:11:51,636 But two of you, if you would 271 00:11:51,636 --> 00:11:52,846 like to come up for just a moment. 272 00:11:54,016 --> 00:11:54,726 Volunteers. 273 00:11:54,846 --> 00:11:55,196 >> I'll do it. 274 00:11:55,546 --> 00:11:56,306 >> Yep, come on down. 275 00:11:56,306 --> 00:11:58,496 And how about in the purple there. 276 00:11:58,546 --> 00:12:00,666 Yep, purple, pink, come on down. 277 00:12:01,586 --> 00:12:03,816 Oh, and darker purple, okay, darker purple. 278 00:12:04,106 --> 00:12:05,106 Okay. Three volunteers. 279 00:12:05,106 --> 00:12:07,146 I will make up a third row somehow spontaneously here. 280 00:12:07,596 --> 00:12:07,956 All right. 281 00:12:08,116 --> 00:12:10,196 So why don't you have a seat on the chair 282 00:12:10,316 --> 00:12:13,016 on the right-hand side there. 283 00:12:13,236 --> 00:12:13,736 All right. 284 00:12:13,906 --> 00:12:17,356 And why don't you have a seat in the second chair. 285 00:12:17,356 --> 00:12:19,576 And why don't you come here and wing this with me. 286 00:12:19,576 --> 00:12:21,296 So what are your names? 287 00:12:22,106 --> 00:12:22,576 >> Christiana [Assumed Spelling]. 288 00:12:22,656 --> 00:12:23,136 >> Christiana. 289 00:12:23,236 --> 00:12:23,426 And? 290 00:12:23,886 --> 00:12:24,296 >> Jordan. 291 00:12:24,526 --> 00:12:24,756 >> Jordan. 292 00:12:24,756 --> 00:12:24,886 And? 293 00:12:24,886 --> 00:12:25,366 >> Karen. 294 00:12:26,466 --> 00:12:27,486 >> And Karen. 295 00:12:27,486 --> 00:12:29,866 Okay. So just to illustrate this point, 296 00:12:29,866 --> 00:12:32,846 and then we'll actually use this in a co-oriented environment. 297 00:12:33,086 --> 00:12:36,286 Suppose that Christiana is a function. 298 00:12:36,286 --> 00:12:38,206 Let's call her, get int, for today. 299 00:12:38,436 --> 00:12:40,266 Jordan's going to play the role of human. 300 00:12:40,526 --> 00:12:43,826 And so I have equipped my function with a whole bunch 301 00:12:43,826 --> 00:12:46,726 of memory, which we'll implement in the form of old-school paper. 302 00:12:47,486 --> 00:12:49,246 We will equip Jordan, meanwhile, 303 00:12:49,246 --> 00:12:51,116 with a keyboard or an old-school pen. 304 00:12:52,226 --> 00:12:54,856 And now Christiana if you would -- your goal -- 305 00:12:54,896 --> 00:12:57,166 your role here -- no, let's see. 306 00:12:58,286 --> 00:12:59,506 I'm still trying to work out a role for you 307 00:12:59,506 --> 00:13:01,326 so hang on for one sec. 308 00:13:01,326 --> 00:13:03,796 So your role in here in this demo is 309 00:13:03,856 --> 00:13:05,396 to get an int from Jordan. 310 00:13:05,396 --> 00:13:10,686 So go ahead and prompt Jordan for an int. 311 00:13:10,686 --> 00:13:12,276 Say something like, give me an int. 312 00:13:13,146 --> 00:13:14,726 >> Give me an int [Laughter]. 313 00:13:14,726 --> 00:13:14,926 >> All right. 314 00:13:14,956 --> 00:13:17,276 So Jordan, playing the role of human is going to type 315 00:13:17,276 --> 00:13:18,936 or write something down on this piece of paper, 316 00:13:19,146 --> 00:13:21,386 hand it back to, get int. 317 00:13:21,876 --> 00:13:24,776 So inside of get int's memory, shall we say, is this, in fact, 318 00:13:24,776 --> 00:13:26,456 int, which is the number 10. 319 00:13:26,546 --> 00:13:28,176 It looks at the [Inaudible] but we don't know that yet. 320 00:13:28,436 --> 00:13:29,736 And here I finally have a role for you. 321 00:13:29,736 --> 00:13:33,216 So now if Christiana doesn't actually -- 322 00:13:33,216 --> 00:13:35,846 if either programmer or who's programming these little puzzle 323 00:13:35,846 --> 00:13:38,886 pieces, don't actually do anything with this return value, 324 00:13:38,886 --> 00:13:42,196 and I call, get int, she wants to hand this piece of paper 325 00:13:42,196 --> 00:13:44,686 to me, but if I don't actually assign -- 326 00:13:44,926 --> 00:13:46,696 no, no, I just messed up the demo. 327 00:13:46,696 --> 00:13:50,446 So if you don't actually assign to something, 328 00:13:50,446 --> 00:13:52,156 it falls on the floor and the value goes away. 329 00:13:52,156 --> 00:13:54,606 It was a complete waste of time, unless the goal 330 00:13:54,606 --> 00:13:56,566 in life was simply to pester the user for some input. 331 00:13:56,816 --> 00:14:00,066 So now if we actually introduce the variable X, 332 00:14:00,216 --> 00:14:02,276 welcome X. Let's do this again. 333 00:14:02,276 --> 00:14:03,296 Let's try a different number just 334 00:14:03,296 --> 00:14:04,816 to make the demo more interesting. 335 00:14:05,126 --> 00:14:05,906 Get int, if you will. 336 00:14:06,146 --> 00:14:06,706 >> Give me an int. 337 00:14:07,516 --> 00:14:14,566 [ Demonstrating ] 338 00:14:15,066 --> 00:14:15,236 >> All right. 339 00:14:15,236 --> 00:14:16,366 This time we have the number 100. 340 00:14:16,366 --> 00:14:18,586 And so this time when you hand it back, this value, 341 00:14:18,586 --> 00:14:20,186 I store it in -- we'll say X, 342 00:14:20,316 --> 00:14:23,616 and now we actually have retained the value. 343 00:14:23,616 --> 00:14:25,936 So I realize that this was among a more childish demonstrations 344 00:14:25,936 --> 00:14:27,336 that I could come up with to illustrate this point. 345 00:14:27,536 --> 00:14:31,016 But odds are you'll never again do that on the screen. 346 00:14:31,016 --> 00:14:33,066 So a big round of applause for our volunteers, if we could. 347 00:14:34,656 --> 00:14:36,176 [Applause] You can keep that for a souvenir, if you'd like. 348 00:14:36,336 --> 00:14:38,506 And if you could just meet up with the teaching fellows 349 00:14:38,506 --> 00:14:40,706 for our little -- our legal department, that would be great. 350 00:14:41,136 --> 00:14:41,446 All right. 351 00:14:41,446 --> 00:14:43,046 So there's one other detail. 352 00:14:44,026 --> 00:14:44,936 >> Right here. 353 00:14:45,686 --> 00:14:48,956 >> Yeah. Right this way. 354 00:14:48,956 --> 00:14:49,446 >> Oh, okay. 355 00:14:49,596 --> 00:14:50,696 >> Thank you Number Three. 356 00:14:50,806 --> 00:14:51,116 All right. 357 00:14:51,316 --> 00:14:57,036 If someone could figure out how to turn off messages, 358 00:14:57,136 --> 00:14:58,246 that would be great [laughter]. 359 00:14:58,246 --> 00:15:00,666 So this will be an interesting class. 360 00:15:00,666 --> 00:15:03,426 So the second common thing that we saw was a syntax like this. 361 00:15:03,426 --> 00:15:04,286 Right. So right ideas. 362 00:15:04,286 --> 00:15:05,616 You're definitely borrowing some 363 00:15:05,616 --> 00:15:06,806 of the pieces that we've looked at. 364 00:15:06,806 --> 00:15:09,276 The curly braces to encapsulate one or more lines of code. 365 00:15:09,276 --> 00:15:10,736 The semicolons to terminate lines. 366 00:15:10,976 --> 00:15:12,876 The parentheses to pass in inputs 367 00:15:12,876 --> 00:15:13,956 or arguments to functions. 368 00:15:14,296 --> 00:15:15,866 But if you throw them altogether, 369 00:15:15,866 --> 00:15:17,006 things tend not to work. 370 00:15:17,006 --> 00:15:17,716 All right. 371 00:15:17,716 --> 00:15:21,366 So this is a thing, Week Number Four, saw very commonly, 372 00:15:22,236 --> 00:15:26,286 which was -- this is something we saw commonly 373 00:15:26,286 --> 00:15:27,936 and there's a couple of issues here. 374 00:15:27,936 --> 00:15:30,266 And this isn't, you know, to point out the flaws 375 00:15:30,266 --> 00:15:31,816 in someone specific, but to point out some 376 00:15:31,816 --> 00:15:32,736 of the common mistakes. 377 00:15:32,736 --> 00:15:34,906 So what's one mistake that's apparent to some here? 378 00:15:36,406 --> 00:15:38,336 Point out what makes you be the obvious. 379 00:15:39,036 --> 00:15:40,626 What is here that shouldn't be here? 380 00:15:41,016 --> 00:15:42,116 [ Inaudible ] 381 00:15:42,116 --> 00:15:42,396 >> All right. 382 00:15:42,396 --> 00:15:43,756 So we don't need the curly braces. 383 00:15:43,756 --> 00:15:46,996 The curly braces, really, we've only deployed in contexts 384 00:15:47,156 --> 00:15:50,806 like loops, wild loops, do wild loops, four loops, where we want 385 00:15:50,806 --> 00:15:52,846 to encapsulate one or more lines of code, 386 00:15:53,086 --> 00:15:55,366 all inside of some conceptual idea 387 00:15:55,436 --> 00:15:56,936 like a condition, like a loop. 388 00:15:57,226 --> 00:16:00,126 Here, print F, recalls a statement and just 389 00:16:00,126 --> 00:16:02,846 like in scratch, statements were these one-liners. 390 00:16:02,846 --> 00:16:06,166 I think they're purple or so in scratch, or yeah, purple, 391 00:16:06,166 --> 00:16:08,516 I think, generally; say hi, for two seconds. 392 00:16:08,906 --> 00:16:11,186 Right. So similarly, if you've got one line of code 393 00:16:11,366 --> 00:16:13,506 with a semicolon at the end saying, this is the end 394 00:16:13,506 --> 00:16:16,106 of the statement, it doesn't really make sense to continue 395 00:16:16,106 --> 00:16:18,476 that conversation to try to continue conceptually 396 00:16:18,476 --> 00:16:20,016 that puzzle piece with curly braces. 397 00:16:20,286 --> 00:16:23,806 Now as an aside, this may very well compile. 398 00:16:23,806 --> 00:16:25,426 In fact, this example would compile 399 00:16:25,606 --> 00:16:30,076 because you can use curly braces in this way to encapsulate lines 400 00:16:30,076 --> 00:16:34,786 of code, but realize for now, that, not good. 401 00:16:34,866 --> 00:16:37,596 Only use these for conditions, only use these for loops, 402 00:16:37,596 --> 00:16:39,546 and it is, in fact, correct to put the semicolon 403 00:16:39,546 --> 00:16:42,986 in on the end here but not, for instance, in a case like this; 404 00:16:43,136 --> 00:16:46,606 if I, by contrast, do it for int, I get zero; 405 00:16:46,606 --> 00:16:51,176 I is less than 10, I plus, plus; and then I do this, 406 00:16:51,176 --> 00:16:53,626 because I'm in the habit of ending my lines with semicolons, 407 00:16:53,626 --> 00:16:56,906 and now I go inside my loop like I was -- 408 00:16:56,906 --> 00:16:58,826 like I learned to do and I do something, 409 00:16:59,396 --> 00:17:00,756 there's a problem with this. 410 00:17:01,246 --> 00:17:01,526 All right. 411 00:17:01,526 --> 00:17:03,496 So it's definitely the semicolon. 412 00:17:03,686 --> 00:17:05,306 But what does the -- what is the effect 413 00:17:05,306 --> 00:17:07,186 of that semicolon would you conjecture? 414 00:17:07,336 --> 00:17:08,826 Yeah. Let's go here. 415 00:17:08,826 --> 00:17:09,396 >> It just ends. 416 00:17:09,396 --> 00:17:20,746 It stops looking for more things to use. 417 00:17:21,216 --> 00:17:22,176 It tries to evaluate the loop as [Inaudible]. 418 00:17:22,176 --> 00:17:23,026 >> Okay. So that's true. 419 00:17:23,026 --> 00:17:25,266 It ultimately evaluates that line of code 420 00:17:25,266 --> 00:17:27,946 but just once the stuff inside the curly braces, 421 00:17:27,986 --> 00:17:29,516 but the semicolon on the end, 422 00:17:29,776 --> 00:17:32,046 essentially has the effect of this. 423 00:17:32,596 --> 00:17:33,836 Go ahead and define your loop. 424 00:17:35,016 --> 00:17:39,506 Do nothing, close your loop and then down here do something. 425 00:17:39,506 --> 00:17:41,936 So in other words, if you terminate these lines early, 426 00:17:41,936 --> 00:17:44,456 if you terminate the four loop early, if you terminate an 427 00:17:44,516 --> 00:17:46,506 if condition early with that semicolon, 428 00:17:46,726 --> 00:17:48,916 the code may very well work; and this is a common source 429 00:17:48,916 --> 00:17:50,926 of frustration and conundrum because you don't -- 430 00:17:50,926 --> 00:17:53,206 because your code looks right and yet it's misbehaving, 431 00:17:53,406 --> 00:17:55,716 but that semicolon means stop looking 432 00:17:55,716 --> 00:17:57,716 for subsequent lines of related code. 433 00:17:58,016 --> 00:18:00,526 So if you do include it up top, the subsequent lines, 434 00:18:00,526 --> 00:18:02,616 even if you have these curly braces, are not, 435 00:18:02,616 --> 00:18:04,126 in fact, going to help you. 436 00:18:04,126 --> 00:18:07,086 So the take-away for now, I would say, -- 437 00:18:07,166 --> 00:18:09,106 is this all software work, yes, good. 438 00:18:09,516 --> 00:18:14,016 The take-away for now is really to look back at some 439 00:18:14,016 --> 00:18:16,046 of the examples we've seen in lecture, some of the examples 440 00:18:16,046 --> 00:18:17,616 that you've now seen in the super sections 441 00:18:17,616 --> 00:18:19,726 and in the future sections really, for guidance. 442 00:18:19,726 --> 00:18:21,266 It's deliberate that a lot of the examples 443 00:18:21,266 --> 00:18:23,126 in lecture are fairly short and bite-size 444 00:18:23,406 --> 00:18:27,546 so that you can actually focus on pulling out the key ideas 445 00:18:27,546 --> 00:18:29,716 without getting distracted by line and lines of code. 446 00:18:29,926 --> 00:18:31,726 And I'll point out as we proceed, because I kind of have 447 00:18:31,756 --> 00:18:32,896 to start using the cloud here. 448 00:18:33,136 --> 00:18:35,936 It's your user name that appears in these messages. 449 00:18:36,086 --> 00:18:39,386 So we'll say hello to you later, if need be. 450 00:18:39,976 --> 00:18:43,546 So with that said, scary voice Number Two. 451 00:18:43,546 --> 00:18:44,276 All right. 452 00:18:44,636 --> 00:18:46,246 So functions and return values. 453 00:18:46,246 --> 00:18:48,196 So just to hammer home this point, for now, 454 00:18:48,196 --> 00:18:50,326 a function really is like a black box. 455 00:18:50,326 --> 00:18:53,216 It's something that someone else wrote or maybe you wrote, 456 00:18:53,396 --> 00:18:55,256 but you can encapsulate all of your ideas 457 00:18:55,256 --> 00:18:57,616 or your functionality inside this thing called a function. 458 00:18:57,896 --> 00:19:00,376 And its purpose in life is to maybe take input 459 00:19:00,646 --> 00:19:02,006 and maybe produce output. 460 00:19:02,236 --> 00:19:04,986 But if it does take input, you have to provide it in the form 461 00:19:04,986 --> 00:19:09,116 of arguments or parameters, like print F takes, generally, 462 00:19:09,116 --> 00:19:10,656 inside of those parentheses. 463 00:19:10,656 --> 00:19:13,306 And if it returns a value, you've got to do something 464 00:19:13,306 --> 00:19:15,496 with that return value too, much like we did up here. 465 00:19:15,666 --> 00:19:17,026 You got to store it in a variable. 466 00:19:17,026 --> 00:19:20,296 You have to pass it as input to another function, as you'll see 467 00:19:20,296 --> 00:19:22,776 as possible in problem's Set One specification, 468 00:19:23,166 --> 00:19:24,776 but it's ultimately up to you. 469 00:19:24,776 --> 00:19:25,766 So let's take a look 470 00:19:26,046 --> 00:19:28,156 if you still have your printouts from Monday. 471 00:19:28,156 --> 00:19:31,776 We have a couple of examples in -- among those printouts, 472 00:19:31,776 --> 00:19:37,536 this one called, return one dot C. So in here, 473 00:19:37,816 --> 00:19:39,966 you'll notice a couple of key things: so one, 474 00:19:39,966 --> 00:19:42,746 a wave of the hand, for the moment, at this int arg C, 475 00:19:42,746 --> 00:19:45,636 and this char Arg V. We'll come back to that today 476 00:19:45,636 --> 00:19:48,186 when we introduce more formally the idea of arrays 477 00:19:48,186 --> 00:19:49,416 and command line arguments, 478 00:19:49,746 --> 00:19:51,526 but there's a couple of other curiosities. 479 00:19:51,526 --> 00:19:53,636 For some reason, at the top of this file, 480 00:19:53,956 --> 00:19:56,956 I've also included what I've called a function prototype. 481 00:19:57,356 --> 00:19:59,716 So we'll see in a moment why that's necessary. 482 00:19:59,776 --> 00:20:01,906 And, in fact, it kind of looks like a function, 483 00:20:01,906 --> 00:20:03,996 and yet here's a semicolon I just said that can get you 484 00:20:03,996 --> 00:20:06,286 into trouble, and yet that's not actually a function, 485 00:20:06,286 --> 00:20:09,346 because there's no interesting lines of code, because it turns 486 00:20:09,346 --> 00:20:12,016 out the interesting code is down here at the bottom. 487 00:20:12,336 --> 00:20:13,846 So it turns out that, main, yes, 488 00:20:14,046 --> 00:20:16,476 is the default function you write, at least in C, 489 00:20:16,636 --> 00:20:17,726 when writing a program. 490 00:20:17,726 --> 00:20:20,206 And the computer just knows that when you run a program, 491 00:20:20,396 --> 00:20:22,936 it should look, by default, for a function called main, 492 00:20:23,216 --> 00:20:25,246 as opposed to one called print F or anything else 493 00:20:25,576 --> 00:20:28,726 for a function called main and execute that one and then it's 494 00:20:28,726 --> 00:20:30,746 up to you to call other functions 495 00:20:30,796 --> 00:20:32,126 like print F, if you want to. 496 00:20:32,306 --> 00:20:34,526 Well, you don't have to just take what you've been handed 497 00:20:34,526 --> 00:20:36,706 in life, functions like print F or round 498 00:20:36,926 --> 00:20:38,346 or maybe other functions you've seen 499 00:20:38,346 --> 00:20:40,266 in super sections or by poking around. 500 00:20:40,476 --> 00:20:42,156 You can certainly implement your own. 501 00:20:42,406 --> 00:20:44,976 Now this is not a particularly compelling pace just 502 00:20:44,976 --> 00:20:46,986 yet of implementing your own function. 503 00:20:47,196 --> 00:20:50,346 But let's suppose that I realize that I'm doing a lot of addition 504 00:20:50,576 --> 00:20:54,626 in my programs, and I actually want to factor out what seems 505 00:20:54,626 --> 00:20:56,146 to becoming a lot of copy, paste. 506 00:20:56,146 --> 00:20:56,966 I keep seeing addition 507 00:20:56,966 --> 00:20:58,986 and I keep just copying my code, copying my code. 508 00:20:59,166 --> 00:21:02,176 Anytime you find yourself in this pattern, odds are, 509 00:21:02,176 --> 00:21:05,126 what you're doing is a candidate for being factored out, 510 00:21:05,126 --> 00:21:07,656 hierarchal decompositions is the buzzword you'd see 511 00:21:07,656 --> 00:21:09,636 in the textbooks if you're following along with any 512 00:21:09,636 --> 00:21:11,766 of them, but it's really just the idea of taking 513 00:21:11,766 --> 00:21:13,946 out common code and putting it into just one place. 514 00:21:14,306 --> 00:21:15,616 So how do you define a function? 515 00:21:15,876 --> 00:21:19,256 We have to decide three things: one, the name of the function. 516 00:21:19,316 --> 00:21:21,846 And just as with variables, you should use some common sense, 517 00:21:21,846 --> 00:21:24,236 some style here, and the function's name should 518 00:21:24,356 --> 00:21:27,056 communicate what it does, calling it X or Y 519 00:21:27,056 --> 00:21:29,026 or Z is generally not all that helpful. 520 00:21:29,316 --> 00:21:31,736 It then should take an argument or parameter 521 00:21:31,866 --> 00:21:34,426 if you want your function to take input. 522 00:21:34,546 --> 00:21:36,426 If you want it to take a so-called argument 523 00:21:36,426 --> 00:21:39,106 or a parameter, there's a subtle semantic distinction 524 00:21:39,106 --> 00:21:41,456 between arguments and parameter, but for all intents 525 00:21:41,456 --> 00:21:42,826 and purposes, they're the same thing. 526 00:21:42,826 --> 00:21:45,226 An argument or a parameter is an input to a function. 527 00:21:45,546 --> 00:21:47,166 Now in this case, it's clear 528 00:21:47,166 --> 00:21:50,186 that I'm not only saying this function takes an input, 529 00:21:50,186 --> 00:21:52,326 I also have to give that input a name. 530 00:21:52,576 --> 00:21:55,276 Because if I don't specify a name for this input, 531 00:21:55,546 --> 00:21:57,366 I have no way of actually referring to it 532 00:21:57,366 --> 00:21:58,726 in subsequent lines of code. 533 00:21:59,016 --> 00:22:02,036 Now I seem to be violating my own mantra here 534 00:22:02,036 --> 00:22:04,896 by just calling this variable A, but this is reasonable, 535 00:22:04,896 --> 00:22:07,536 much as with four loops when you just call an incrementing 536 00:22:07,536 --> 00:22:10,486 variable I, because you just need an index, well, same here. 537 00:22:10,486 --> 00:22:12,916 If you're writing a one line function, whose sole purpose 538 00:22:12,916 --> 00:22:16,546 in life is addition, and, clearly, this is about addition, 539 00:22:16,716 --> 00:22:17,806 well, then it's, in fact, 540 00:22:17,806 --> 00:22:20,736 reasonable to use a shorter symbol like A, 541 00:22:20,996 --> 00:22:22,576 but I do have to specify it's type. 542 00:22:22,936 --> 00:22:25,136 So here's where C differs from some language 543 00:22:25,136 --> 00:22:27,036 that you might be familiar with and from languages 544 00:22:27,036 --> 00:22:29,696 that you'll see later in the course, like PHP and JavaScript. 545 00:22:29,986 --> 00:22:32,346 You have to be ever so explicit 546 00:22:32,576 --> 00:22:34,606 as to what data type your actually passing in. 547 00:22:35,066 --> 00:22:36,556 So that's one, the name. 548 00:22:36,706 --> 00:22:38,886 Two, the arguments or parameters, 549 00:22:38,886 --> 00:22:40,966 and three, is the return value. 550 00:22:40,966 --> 00:22:43,796 So you're implementing this black box, and if its purpose 551 00:22:43,796 --> 00:22:45,726 in life is to actually return a value, 552 00:22:45,946 --> 00:22:48,626 you have to tell the compiler what kind of value to expect, 553 00:22:48,626 --> 00:22:50,306 and this is going to have ripple effects. 554 00:22:50,576 --> 00:22:54,756 If this function increment is defined as or declared as, 555 00:22:54,806 --> 00:22:58,206 as we say, returning an int, you have to make sure 556 00:22:58,206 --> 00:23:00,526 that if you're doing something with the return value, 557 00:23:00,756 --> 00:23:04,216 that you're storing it in the same type of the variable, 558 00:23:04,216 --> 00:23:06,636 like an int variable or you're passing it to print F 559 00:23:06,636 --> 00:23:09,606 with percent D that you're using it in the appropriate context, 560 00:23:09,696 --> 00:23:12,576 unless, as an aside, you intend to typecast it, 561 00:23:12,646 --> 00:23:14,016 which we've discussed, if briefly. 562 00:23:14,326 --> 00:23:14,606 All right. 563 00:23:14,606 --> 00:23:16,836 So what's for all this function -- 564 00:23:16,836 --> 00:23:18,136 what's the role of this function in life? 565 00:23:18,446 --> 00:23:20,696 It returns its argument plus one, that's it. 566 00:23:20,886 --> 00:23:22,456 Now it's a stupid little function. 567 00:23:22,456 --> 00:23:25,046 We could certainly implement this much more efficiently 568 00:23:25,046 --> 00:23:27,226 by just writing this one line of code, but, again, 569 00:23:27,226 --> 00:23:30,496 the key here is to take this step toward decomposing our code 570 00:23:30,496 --> 00:23:32,176 into chunks of code. 571 00:23:32,176 --> 00:23:33,466 So much like you did was scratch, 572 00:23:33,466 --> 00:23:35,816 some of you might have implemented these very 573 00:23:35,816 --> 00:23:38,616 unwielding programs, and that's fine because you're kind 574 00:23:38,616 --> 00:23:39,856 of constrained in scratch. 575 00:23:39,856 --> 00:23:40,806 You don't have these kinds 576 00:23:40,806 --> 00:23:42,506 of design capabilities to the same extent. 577 00:23:42,826 --> 00:23:45,916 But a lot of you did realize you can have multiple one green flag 578 00:23:45,996 --> 00:23:48,526 clicked scripts and so you can factor things 579 00:23:48,526 --> 00:23:51,096 out to have different sorts of roles; so same idea here, 580 00:23:51,306 --> 00:23:53,986 even though in scratch you can not call functions 581 00:23:53,986 --> 00:23:55,056 that you yourself wrote. 582 00:23:55,056 --> 00:23:56,656 There's no notion of naming a script 583 00:23:57,086 --> 00:23:59,226 as you can a function in scratch. 584 00:23:59,386 --> 00:24:00,496 So here's the main function. 585 00:24:00,496 --> 00:24:01,746 What does this thing do? 586 00:24:01,996 --> 00:24:04,566 Int main. Lets ignore what's inside the parentheses for now. 587 00:24:04,846 --> 00:24:08,766 Int X gets two; so that declares four bytes of memory 588 00:24:08,766 --> 00:24:10,486 and stores inside of them the Number 2. 589 00:24:10,836 --> 00:24:11,816 This is just aesthetics. 590 00:24:11,916 --> 00:24:13,566 X is now placeholder. 591 00:24:13,566 --> 00:24:14,476 What do I place? 592 00:24:14,646 --> 00:24:17,806 The value of X. Now I say, incrementing dot, dot, dot, 593 00:24:17,806 --> 00:24:19,636 just to convey the idea of some progress, 594 00:24:19,636 --> 00:24:22,486 and now here is a so-called function call. 595 00:24:22,596 --> 00:24:24,066 We've seen function calls before. 596 00:24:24,066 --> 00:24:26,846 You've used get int, get string, or the like perhaps already. 597 00:24:27,136 --> 00:24:29,546 Well, here, I'm calling my own function, increment. 598 00:24:29,916 --> 00:24:31,866 It's defined later down which is fine. 599 00:24:32,446 --> 00:24:36,136 I'm passing an X, and yet I'm also assigning the return value 600 00:24:36,136 --> 00:24:38,966 to X. So just intuitively what's going to be the effect 601 00:24:38,966 --> 00:24:39,976 of this one line of code? 602 00:24:40,516 --> 00:24:42,576 [ Inaudible ] 603 00:24:43,076 --> 00:24:44,096 >> Right. So to do X plus, plus. 604 00:24:44,096 --> 00:24:47,546 So, again, this is why I'm disclaiming that this isn't all 605 00:24:47,546 --> 00:24:49,686 that interesting yet, but what am I doing. 606 00:24:49,686 --> 00:24:52,696 I'm passing as input the value in X, which is two. 607 00:24:53,156 --> 00:24:54,966 I am then doing some mathematics with it, 608 00:24:55,006 --> 00:24:56,256 clearly, based on the below. 609 00:24:56,416 --> 00:24:58,426 I'm returning the result of that summation; 610 00:24:58,626 --> 00:25:01,266 so it's probably now three, but when I get that three back, 611 00:25:01,466 --> 00:25:04,606 if I don't actually do anything with it, like assign it, 612 00:25:04,806 --> 00:25:07,816 it's all for not, and so I decide arbitrarily here 613 00:25:07,816 --> 00:25:10,416 to assign it back to X, and so the end result, 614 00:25:10,666 --> 00:25:12,916 in this particular case, is indeed, 615 00:25:12,966 --> 00:25:15,786 identical to saying X plus, plus, 616 00:25:15,946 --> 00:25:18,786 but now we have the framework in mind or the capabilities 617 00:25:18,786 --> 00:25:21,506 and code to start doing more interesting things 618 00:25:21,746 --> 00:25:22,576 with functions like this. 619 00:25:22,576 --> 00:25:24,676 And actually, if I don't want to clobber, as we say, 620 00:25:24,676 --> 00:25:26,516 overwrite the value of my variable, 621 00:25:26,796 --> 00:25:29,566 I could declare another one and store the return value in Y; 622 00:25:29,856 --> 00:25:32,626 so now I have two ints in memory; X and Y, 623 00:25:32,856 --> 00:25:34,516 one with two, one with three. 624 00:25:34,816 --> 00:25:36,826 But in the end, because I'm taking this approach, 625 00:25:37,046 --> 00:25:39,186 if I proceed with the remaining two lines, 626 00:25:39,186 --> 00:25:41,916 it says incremented X is now hopefully three 627 00:25:42,286 --> 00:25:44,206 and that should be a working program. 628 00:25:44,206 --> 00:25:44,966 So let's take a look. 629 00:25:44,966 --> 00:25:47,946 Let me go ahead and compile return one. 630 00:25:48,526 --> 00:25:48,786 All right. 631 00:25:48,786 --> 00:25:49,846 Everything seems to work. 632 00:25:49,846 --> 00:25:52,006 So let's go ahead and run return one, enter. 633 00:25:52,556 --> 00:25:53,936 So it does, in fact, seem to work. 634 00:25:53,936 --> 00:25:57,206 But there's a key line of code that I lost over a moment ago 635 00:25:57,476 --> 00:25:58,506 and that was this one here. 636 00:25:58,506 --> 00:26:01,116 I have my includes, standard IO's; so I can use print F, 637 00:26:01,346 --> 00:26:03,196 but then I had this thing called a prototype. 638 00:26:03,196 --> 00:26:05,966 Let me actually get rid of that for just a moment. 639 00:26:06,316 --> 00:26:10,456 Re-save my file, and actually now recompile with make. 640 00:26:11,516 --> 00:26:13,536 So now I've got an issue all of a sudden. 641 00:26:13,536 --> 00:26:16,586 I just reran make or GCC does it for me, 642 00:26:16,796 --> 00:26:17,986 and now I've got all these errors. 643 00:26:17,986 --> 00:26:18,456 Well, let's see. 644 00:26:18,456 --> 00:26:20,306 In function, main. 645 00:26:20,376 --> 00:26:20,786 All right. 646 00:26:20,786 --> 00:26:21,476 That's not helpful yet. 647 00:26:21,476 --> 00:26:22,266 Okay. Interesting. 648 00:26:22,496 --> 00:26:24,666 So here's a tip if you've not picked up on this already. 649 00:26:24,666 --> 00:26:27,466 If you're getting these compilation errors, well, 650 00:26:27,636 --> 00:26:30,496 when you compile, notice there's a number of key help -- 651 00:26:30,496 --> 00:26:32,456 the number of helpful tidbits here. 652 00:26:32,646 --> 00:26:34,126 So one, is obviously the filename 653 00:26:34,126 --> 00:26:34,906 that I've been working on. 654 00:26:35,116 --> 00:26:37,516 Colon 21, though, for those who are already familiar, 655 00:26:37,946 --> 00:26:39,016 is a line number; right? 656 00:26:39,016 --> 00:26:41,006 And nano, though it's a primitive program, 657 00:26:41,006 --> 00:26:43,336 we deliberately turned on a feature where if you scroll up 658 00:26:43,336 --> 00:26:45,436 and down, it literally tells you at bottom left, 659 00:26:45,746 --> 00:26:48,226 what line you're on; so that can help you narrow 660 00:26:48,326 --> 00:26:50,116 in on the problem you're having. 661 00:26:50,116 --> 00:26:51,106 What was that problem? 662 00:26:51,376 --> 00:26:52,216 Let me move it higher up. 663 00:26:52,986 --> 00:26:53,826 What was that problem? 664 00:26:54,116 --> 00:26:57,136 Implicit declaration of function increment. 665 00:26:57,676 --> 00:27:00,036 So here's where C is kind of showing its age. 666 00:27:00,036 --> 00:27:02,916 It's kind of a stupid lack of feature that this is the case, 667 00:27:03,186 --> 00:27:05,526 but notice that here is main, at the top of my file. 668 00:27:05,526 --> 00:27:06,976 I like it at the top of my file. 669 00:27:06,976 --> 00:27:08,536 It's the first thing that gets executed. 670 00:27:08,536 --> 00:27:10,686 It makes sense to me logically that I want to see it first 671 00:27:10,686 --> 00:27:12,946 when I open the file; so I scroll on down 672 00:27:13,476 --> 00:27:16,006 but only later is increment declared. 673 00:27:16,196 --> 00:27:19,756 So C or the compiler's for C are kind of dumb, by definition 674 00:27:19,756 --> 00:27:23,076 of the language years ago, whereby, they only know 675 00:27:23,076 --> 00:27:25,006 about what they've already seen. 676 00:27:25,206 --> 00:27:28,886 So even though increment is obviously declared and defined 677 00:27:28,886 --> 00:27:31,526 in this function, it's later; so it's too late 678 00:27:31,646 --> 00:27:32,986 for me to use it in main. 679 00:27:33,346 --> 00:27:35,706 So I have two solutions here; one, what would be sort 680 00:27:35,706 --> 00:27:39,976 of the obvious sort of hackish or solve it fast solution? 681 00:27:40,016 --> 00:27:41,096 [ Inaudible ] 682 00:27:41,096 --> 00:27:42,476 >> Right. Just move it to the top; right? 683 00:27:42,476 --> 00:27:44,026 Delete those lines of code and move them 684 00:27:44,026 --> 00:27:45,766 up to the top and problem solved. 685 00:27:45,766 --> 00:27:49,096 Now downside of this, well, I've just now violated my preference 686 00:27:49,096 --> 00:27:51,486 for having main up top, maybe that's forgivable. 687 00:27:51,806 --> 00:27:55,296 But very soon will you find that if you're writing programs 688 00:27:55,296 --> 00:27:58,266 with lots and lots of functions, and some might call these, 689 00:27:58,266 --> 00:28:01,376 and these might call these, you can come up very soon 690 00:28:01,376 --> 00:28:04,246 with scenarios where you can not order them top to bottom, 691 00:28:04,526 --> 00:28:06,776 because everything is comingled, and there's a lot 692 00:28:06,776 --> 00:28:08,536 of cross talk among all of your functions. 693 00:28:08,776 --> 00:28:12,096 So, thankfully, C does provide a solution for this, whereby, 694 00:28:12,176 --> 00:28:15,496 I can provide a hint, essentially, to the compiler. 695 00:28:15,716 --> 00:28:17,386 It's a little redundant, admittedly, 696 00:28:17,656 --> 00:28:19,536 but I include not the same function, 697 00:28:19,756 --> 00:28:22,356 but just its prototype, as I say in the comment here. 698 00:28:22,386 --> 00:28:23,976 What's the prototype of a function? 699 00:28:24,206 --> 00:28:27,846 The prototype is just its return value, its name, 700 00:28:28,236 --> 00:28:30,346 and its arguments, if any. 701 00:28:30,426 --> 00:28:32,756 But a semicolon thereafter is sufficient. 702 00:28:32,846 --> 00:28:35,146 You don't have to re-implement the function. 703 00:28:35,146 --> 00:28:37,476 You certainly shouldn't change anything about the function. 704 00:28:37,736 --> 00:28:40,476 So now, henceforth, when you say, to declare a function, 705 00:28:40,766 --> 00:28:42,686 I am declaring a function here 706 00:28:43,126 --> 00:28:47,636 but down here am I quote unquote defining it or implementing it. 707 00:28:48,036 --> 00:28:49,356 That's the semantic distinction. 708 00:28:49,426 --> 00:28:49,536 Yeah. 709 00:28:50,326 --> 00:28:55,206 >> How can you name the function [Inaudible]? 710 00:28:55,206 --> 00:28:57,066 >> How can I name it int A but it works for? 711 00:28:57,066 --> 00:28:59,906 >> X, like there's X up here [Inaudible]. 712 00:28:59,906 --> 00:29:00,666 >> Really good question. 713 00:29:00,666 --> 00:29:02,686 We'll explore more of this today, actually. 714 00:29:02,686 --> 00:29:06,076 But this choice of names, both for my functions and also 715 00:29:06,076 --> 00:29:07,726 for my parameters 716 00:29:07,726 --> 00:29:10,436 or my variables is completely up to me. 717 00:29:10,636 --> 00:29:13,106 What's going on underneath the hood is we'll see pictorially 718 00:29:13,106 --> 00:29:17,096 today is that when I declare this X, this variable X here. 719 00:29:17,436 --> 00:29:19,276 Right. I defined this last week. 720 00:29:19,476 --> 00:29:22,636 It's just a little block of memory in RAM, you know, 721 00:29:22,636 --> 00:29:24,986 that represents four bytes, 32-bits 722 00:29:24,986 --> 00:29:26,716 and the Number Two is currently in it. 723 00:29:26,966 --> 00:29:30,586 The fact that I've called it X, is really just a programming -- 724 00:29:31,316 --> 00:29:33,326 it's a feature of the programming language that I, 725 00:29:33,326 --> 00:29:34,586 the human, can now refer to it. 726 00:29:34,776 --> 00:29:38,886 Not by some very arcane numeric address inside my computer's 727 00:29:38,886 --> 00:29:40,646 memory, but by a symbol, by a name. 728 00:29:40,646 --> 00:29:42,576 It's just more useful to refer to it by name. 729 00:29:42,926 --> 00:29:46,526 Well, I don't have to use that name for every copy of X, 730 00:29:46,526 --> 00:29:47,666 and as we'll see in a moment. 731 00:29:47,876 --> 00:29:50,906 When you call a function, what actually happens is 732 00:29:50,906 --> 00:29:55,596 that function gets its own chunk of memory that's identical 733 00:29:55,596 --> 00:29:57,526 in size to the data type that it expects. 734 00:29:57,526 --> 00:29:59,966 And in this case, an int, which means, this function, 735 00:29:59,966 --> 00:30:03,326 increment also gets from the computer its own four bytes. 736 00:30:03,326 --> 00:30:04,706 It's own 32-bits. 737 00:30:04,966 --> 00:30:06,936 And when you call a function, one of the things 738 00:30:06,936 --> 00:30:09,906 that happens is whatever your passing in, for instance, X, 739 00:30:10,076 --> 00:30:13,076 and if this is A, this thing gets copied into A; 740 00:30:13,076 --> 00:30:15,866 so at that moment in time of calling increment, 741 00:30:15,866 --> 00:30:18,846 I actually have two copies of the same value in memory 742 00:30:19,156 --> 00:30:20,676 but they're referred to by different names. 743 00:30:21,006 --> 00:30:24,886 But this is a -- this raises a problem, that we'll see actually 744 00:30:25,226 --> 00:30:26,976 in just a moment having this duplication. 745 00:30:27,516 --> 00:30:31,576 [ Inaudible ] 746 00:30:32,076 --> 00:30:32,716 >> Good question. 747 00:30:32,716 --> 00:30:34,256 So short answer, yes. 748 00:30:34,256 --> 00:30:36,326 You can use this for nefarious purposes. 749 00:30:36,486 --> 00:30:39,326 Not so much with int, because ints are bounded in size. 750 00:30:39,326 --> 00:30:41,376 They are 32-bits or they are four bytes. 751 00:30:41,696 --> 00:30:43,866 But once we get the strings today, 752 00:30:43,866 --> 00:30:45,076 in arrays then absolutely. 753 00:30:45,076 --> 00:30:47,526 This is how a lot, a lot, a lot of software 754 00:30:47,526 --> 00:30:50,206 and servers are still compromised today. 755 00:30:50,716 --> 00:30:53,896 So putting back that prototype allows me to compile this, 756 00:30:53,936 --> 00:30:56,096 because I'm saying proactively to the compiler hey, 757 00:30:56,456 --> 00:30:58,126 you're going to see a function called increment. 758 00:30:58,126 --> 00:30:58,816 It returns an int. 759 00:30:58,816 --> 00:31:01,506 It takes an int; deal with it when you get to it 760 00:31:01,506 --> 00:31:03,976 and then once you look through the whole file, 761 00:31:04,226 --> 00:31:07,486 should you find -- ultimately find the actual code 762 00:31:07,486 --> 00:31:09,666 that defines or impliments this thing. 763 00:31:09,906 --> 00:31:11,726 Well, let's look at a slightly different version of this. 764 00:31:11,726 --> 00:31:15,276 So in return to, we have a slight variance here, 765 00:31:15,956 --> 00:31:18,796 where I'm defining apparently -- 766 00:31:18,796 --> 00:31:20,286 declaring a function called cube. 767 00:31:20,346 --> 00:31:22,786 So let's take a look down here at what I'm doing 768 00:31:23,196 --> 00:31:28,756 and notice here that this is perhaps more compelling; right? 769 00:31:28,756 --> 00:31:30,806 Doing incrementation, pretty useless; right? 770 00:31:30,806 --> 00:31:32,016 Because I can just use plus, plus. 771 00:31:32,016 --> 00:31:34,636 I could have saved myself five minutes of verbal discussion. 772 00:31:34,976 --> 00:31:37,176 But with something like cube, now it's starting 773 00:31:37,176 --> 00:31:38,726 to make a little more sense; right? 774 00:31:38,726 --> 00:31:41,906 Not to harp on the mathematical features of this, but cubing, 775 00:31:42,156 --> 00:31:45,866 you know, if you're starting to do AX star, X star, X, 776 00:31:45,926 --> 00:31:48,286 every time you want to cube some value in a program, 777 00:31:48,556 --> 00:31:49,596 it just feels like this is going 778 00:31:49,596 --> 00:31:51,526 to get a little messy looking, if nothing else. 779 00:31:51,526 --> 00:31:55,226 And conceptually, the idea of cubing a value feels 780 00:31:55,226 --> 00:31:58,316 like you could package that up into a little box, a little tool 781 00:31:58,316 --> 00:32:01,276 that takes input, a value, and returns output the answer 782 00:32:01,456 --> 00:32:04,086 so you don't have to constantly do A star, 783 00:32:04,086 --> 00:32:06,856 A star, A, and so forth. 784 00:32:06,886 --> 00:32:08,146 So this program ,at the end of today, 785 00:32:08,146 --> 00:32:09,346 is pretty much the same thing. 786 00:32:09,556 --> 00:32:10,596 I'm taking an input. 787 00:32:10,596 --> 00:32:11,576 I'm returning some value. 788 00:32:11,866 --> 00:32:13,546 But now at least we're a step closer 789 00:32:13,546 --> 00:32:16,406 to doing something that's a little bit more compelling, 790 00:32:16,406 --> 00:32:17,176 but this isn't always a good thing. 791 00:32:17,176 --> 00:32:18,566 So it turns out there's this issue of scope here. 792 00:32:18,596 --> 00:32:19,556 So when you define a function, you have, 793 00:32:19,586 --> 00:32:20,756 as we've seen these curly braces, that define all 794 00:32:20,786 --> 00:32:21,986 of the code that's associated with that function; 795 00:32:22,016 --> 00:32:22,556 open curly brace code, 796 00:32:22,586 --> 00:32:23,546 close curly brace, that is the function. 797 00:32:23,576 --> 00:32:24,566 These functions thus far have been short. 798 00:32:24,596 --> 00:32:25,976 They've just added a number or they've multiplied a number 799 00:32:26,006 --> 00:32:27,446 of couple of times, but they've each had what's called their own 800 00:32:27,476 --> 00:32:28,826 scope, and, by this, I mean the following: If we go ahead 801 00:32:28,856 --> 00:32:30,296 and open up buggy three, the clue already is that there is, 802 00:32:30,326 --> 00:32:31,856 in fact, going to be a problem with this, because my purpose 803 00:32:31,886 --> 00:32:32,936 in life here is a little more interesting, 804 00:32:32,966 --> 00:32:33,446 for whatever reason. 805 00:32:33,476 --> 00:32:34,826 I've decided that I need to be able, in my program, 806 00:32:34,856 --> 00:32:35,816 to swap the value of two variables. 807 00:32:35,846 --> 00:32:36,026 All right. 808 00:32:36,056 --> 00:32:37,106 Now I could do this pretty easily in just 809 00:32:37,136 --> 00:32:37,766 by reasoning through it; right? 810 00:32:37,796 --> 00:32:38,936 If I have two values -- let me go on up here -- 811 00:32:38,966 --> 00:32:40,406 if I've got two values, let's call it int X gets one, 812 00:32:40,436 --> 00:32:41,456 int Y gets two, semicolon at the end. 813 00:32:41,486 --> 00:32:42,686 So now I want to actually swap these values. 814 00:32:42,716 --> 00:32:43,946 You might think -- and this board is -- and you know what, 815 00:32:43,976 --> 00:32:45,446 let's use the computer because it will erase more cleanly 816 00:32:45,476 --> 00:32:45,686 than chalk. 817 00:32:45,716 --> 00:32:46,286 So we just upgraded. 818 00:32:46,316 --> 00:32:46,496 All right. 819 00:32:46,526 --> 00:32:47,966 So now we have int X gets one, -- oh, this is much faster -- 820 00:32:47,996 --> 00:32:49,646 int Y gets two, and now my goal in life is to swap these things. 821 00:32:49,676 --> 00:32:50,906 So now at first glance, you may be a little lazy 822 00:32:50,936 --> 00:32:52,166 and be all right, X gets Y, Y gets X; buck. 823 00:32:52,196 --> 00:32:52,376 All right. 824 00:32:52,406 --> 00:32:52,976 What's the problem here? 825 00:32:53,516 --> 00:32:56,546 [ Inaudible ] 826 00:32:57,046 --> 00:32:58,646 >> The murmuring is correct. 827 00:32:58,646 --> 00:33:05,086 Right. So X get assigned the value of Y, 828 00:33:05,086 --> 00:33:11,706 but then Y gets the value of X; so what have you really done? 829 00:33:11,966 --> 00:33:18,036 Both of them now equal what value? 830 00:33:18,596 --> 00:33:21,516 So both of them now equal? 831 00:33:22,126 --> 00:33:22,336 >> Two. 832 00:33:23,396 --> 00:33:26,966 >> I'll keep asking until we get the right -- 833 00:33:26,966 --> 00:33:31,076 until we all merge on the same one. 834 00:33:31,076 --> 00:33:33,916 So what does the value have? 835 00:33:35,106 --> 00:33:37,496 So X is getting assigned two. 836 00:33:37,576 --> 00:33:45,796 So now X equals two then Y gets assigned the value of X, 837 00:33:45,796 --> 00:33:49,236 which is two; so I think they are, in fact, two; right? 838 00:33:49,236 --> 00:33:50,766 So you're clobbering the value of X 839 00:33:50,766 --> 00:33:58,786 and then you can't get back X's value because it's too late. 840 00:33:58,786 --> 00:33:59,996 All right. 841 00:33:59,996 --> 00:34:01,316 So what's a quick fix? 842 00:34:01,316 --> 00:34:04,036 Well, we know how to declare variables here. 843 00:34:04,036 --> 00:34:07,986 So why don't we just have a temporary one. 844 00:34:07,986 --> 00:34:11,716 It's reasonable because it's meant 845 00:34:11,716 --> 00:34:14,566 for temporary purposes just to call it temp or T-M-P for short, 846 00:34:14,566 --> 00:34:18,946 and I can define temp as -- why don't I set it as equal to X, 847 00:34:18,946 --> 00:34:24,886 initially, because now that I've stored X in a separate chunk 848 00:34:24,886 --> 00:34:29,966 of memory, I can now change its value and then I can say Y gets, 849 00:34:29,966 --> 00:34:34,746 not X, but temp, and so now I've actually swapped these values. 850 00:34:34,746 --> 00:34:35,406 All right. 851 00:34:35,406 --> 00:34:39,706 So you can't do this, because you're going to lose track 852 00:34:39,946 --> 00:34:41,696 of the numbers if you do that. 853 00:34:41,796 --> 00:34:44,266 You actually need to kind of do this 854 00:34:44,266 --> 00:34:46,886 and then you can swap these two numbers 855 00:34:46,886 --> 00:34:49,706 but it requires 50 percent more memory. 856 00:34:49,706 --> 00:34:51,076 It requires this additional int. 857 00:34:51,256 --> 00:34:52,116 All right. 858 00:34:52,706 --> 00:34:58,226 So suppose I want to do this a lot in a function. 859 00:34:58,226 --> 00:35:03,216 It's reasonable to assume that I want 860 00:35:03,216 --> 00:35:09,036 to implement a function called swap, that takes two arguments, 861 00:35:09,036 --> 00:35:12,276 A and B, and its purpose in life is to swap those two values. 862 00:35:12,496 --> 00:35:14,906 We'll, let's see what it does. 863 00:35:14,906 --> 00:35:15,286 All right. 864 00:35:15,286 --> 00:35:19,516 Well, it looks like I just stole my own implementation 865 00:35:19,516 --> 00:35:24,826 but I happen to call it A and B, instead of X and Y, 866 00:35:25,136 --> 00:35:27,456 and so here, what's going on? 867 00:35:27,456 --> 00:35:31,456 Well, one, function has called swap, takes one int called A, 868 00:35:31,676 --> 00:35:33,816 followed by another int called B, and as we've seen 869 00:35:33,816 --> 00:35:35,666 with print F, if you have multiple arguments, 870 00:35:35,666 --> 00:35:36,736 you separate them by commas. 871 00:35:36,866 --> 00:35:37,506 Same deal here. 872 00:35:37,846 --> 00:35:40,116 Now void, we've seen void before. 873 00:35:40,116 --> 00:35:43,316 Void just means this function doesn't return anything. 874 00:35:43,806 --> 00:35:46,056 Its purpose in life is to do something, 875 00:35:46,056 --> 00:35:47,616 to have what we call a side effect, 876 00:35:47,876 --> 00:35:49,506 but it doesn't actually return a value 877 00:35:49,716 --> 00:35:51,406 because functions don't have to have values. 878 00:35:51,606 --> 00:35:53,116 They only need to have values if you care 879 00:35:53,116 --> 00:35:54,896 about getting something back from them, get int, 880 00:35:55,106 --> 00:35:56,746 you care about its value; but swap, 881 00:35:56,746 --> 00:35:58,316 I don't want to hear it again. 882 00:35:58,526 --> 00:36:00,106 Just swap these two values for me 883 00:36:00,106 --> 00:36:02,046 and let me move on is the reasoning. 884 00:36:02,046 --> 00:36:03,786 So here, I say void. 885 00:36:03,786 --> 00:36:04,966 It doesn't return anything. 886 00:36:04,966 --> 00:36:08,466 So it would be incorrect to try to assign this to a variable X 887 00:36:08,466 --> 00:36:11,316 or Y or Z, because it doesn't actually give me anything back. 888 00:36:11,736 --> 00:36:13,956 So this code is, in fact, correct. 889 00:36:14,216 --> 00:36:18,656 This code does swap the two values, A and B. However, 890 00:36:18,656 --> 00:36:20,916 when I run this program, something happens. 891 00:36:21,346 --> 00:36:23,236 I first print what X is. 892 00:36:23,496 --> 00:36:24,586 I then print what Y is. 893 00:36:24,586 --> 00:36:26,196 I then claim I'm swapping. 894 00:36:26,496 --> 00:36:27,946 I then perform the swap. 895 00:36:28,096 --> 00:36:31,256 I then insist, okay, swap, and then I print 896 00:36:31,256 --> 00:36:32,166 out their values again. 897 00:36:32,346 --> 00:36:35,036 So in theory I should see one, two; two, one. 898 00:36:35,036 --> 00:36:36,866 Well, let's actually see what I see. 899 00:36:36,866 --> 00:36:38,216 So let's go ahead and make this. 900 00:36:38,216 --> 00:36:39,226 This is buggy three. 901 00:36:39,226 --> 00:36:41,676 There are no syntax errors so it does compile. 902 00:36:41,676 --> 00:36:44,026 But if I run this thing, buggy three, 903 00:36:44,706 --> 00:36:46,956 they've clearly not been swapped. 904 00:36:47,776 --> 00:36:49,276 So let's do a little sanity check. 905 00:36:49,276 --> 00:36:51,546 Right. When in doubt, let me just use print F for now. 906 00:36:51,546 --> 00:36:53,936 We'll use more sophisticated tools before long, 907 00:36:53,936 --> 00:36:55,216 but let's do a little sanity check. 908 00:36:55,216 --> 00:36:58,896 Let me go ahead and call print F. I'm going to say A equals 909 00:36:58,896 --> 00:37:03,636 and then I need a percent D, backslash, comma A; 910 00:37:03,986 --> 00:37:07,776 so this is a little bit of -- more syntax than would be nice, 911 00:37:08,016 --> 00:37:10,936 but I'm just saying, literally, A equals percent D, 912 00:37:10,936 --> 00:37:12,986 and I'm plugging in the value of A for percent D. 913 00:37:12,986 --> 00:37:13,996 So I have little cheat sheet. 914 00:37:14,276 --> 00:37:16,856 Let me go ahead and do the same thing with B. 915 00:37:18,396 --> 00:37:20,226 So I have a little sanity check now. 916 00:37:20,226 --> 00:37:23,226 Inside of swap itself, I'm printing 917 00:37:23,226 --> 00:37:24,756 out the values, if temporarily. 918 00:37:24,756 --> 00:37:26,146 Let me go ahead and recompile. 919 00:37:26,266 --> 00:37:27,946 It works. Let me go ahead and rerun. 920 00:37:28,776 --> 00:37:30,756 So it does seem to be working; right? 921 00:37:30,796 --> 00:37:32,196 X is 1, Y is 2. 922 00:37:32,196 --> 00:37:33,256 Okay. It worked. 923 00:37:33,256 --> 00:37:37,886 Right. A is 2; B is 1, and yet somehow the process gets undone. 924 00:37:37,886 --> 00:37:40,056 So it's almost as if functions really are useless 925 00:37:40,056 --> 00:37:41,946 because they have no ultimate effect. 926 00:37:42,516 --> 00:37:48,616 [ Inaudible ] 927 00:37:49,116 --> 00:37:49,996 >> Okay. So it's a good thought. 928 00:37:49,996 --> 00:37:51,946 Maybe the problem is that we're -- you -- well, let's see. 929 00:37:51,996 --> 00:37:53,476 We're defining the function at the bottom. 930 00:37:54,516 --> 00:37:58,546 [ Inaudible ] 931 00:37:59,046 --> 00:38:00,546 >> Okay. So there's a couple of fixes. 932 00:38:00,546 --> 00:38:01,576 Let me come back to that one. 933 00:38:01,576 --> 00:38:02,656 So there's a couple of fixes here. 934 00:38:02,656 --> 00:38:04,456 But why is this problem happening? 935 00:38:04,626 --> 00:38:06,396 Well, just a moment ago, we did have -- 936 00:38:06,396 --> 00:38:08,286 and this time I'll use some chalk here -- 937 00:38:08,286 --> 00:38:12,356 a moment ago, we did have a picture depicting what was going 938 00:38:12,356 --> 00:38:14,276 on when you call the function. 939 00:38:14,276 --> 00:38:18,076 And so in main, if we called this the chunk of memory 940 00:38:18,156 --> 00:38:20,726 that main owns, main has, at this point 941 00:38:20,726 --> 00:38:22,156 in the story, two variables. 942 00:38:22,156 --> 00:38:24,856 We called them X and Y, recall, and Y2. 943 00:38:24,856 --> 00:38:27,066 Well, I said int X and int Y; 944 00:38:27,346 --> 00:38:31,326 so that gave me one square here called X, one square here 945 00:38:31,366 --> 00:38:34,776 or wherever, called Y, done, one was put in here; 946 00:38:35,006 --> 00:38:38,146 two was put in here, and then I called this function swap. 947 00:38:38,436 --> 00:38:41,326 So swap, as promised earlier, is a function. 948 00:38:41,326 --> 00:38:43,516 It's given its own memory as much as it asks for. 949 00:38:43,666 --> 00:38:45,086 It asks for how much? 950 00:38:45,596 --> 00:38:47,976 Well, it's actually asking for three things. 951 00:38:47,976 --> 00:38:49,376 They're declared a little differently. 952 00:38:49,716 --> 00:38:52,036 But essentially, this A and this B, 953 00:38:52,036 --> 00:38:54,486 even though we call them semantically parameters 954 00:38:54,486 --> 00:38:57,426 or arguments, really they're just local variables. 955 00:38:57,606 --> 00:39:00,596 So variables you were introduced to in scratch, 956 00:39:00,746 --> 00:39:03,486 and there was this idea of global and local variables, 957 00:39:03,486 --> 00:39:05,156 but scratch called them something a little different. 958 00:39:05,356 --> 00:39:06,916 When you created a variable in scratch, 959 00:39:07,146 --> 00:39:07,826 you could either make it 960 00:39:07,826 --> 00:39:10,406 for this sprite only or for all sprites. 961 00:39:10,706 --> 00:39:10,946 All right. 962 00:39:10,946 --> 00:39:14,676 For this sprite only, means local, only this function, 963 00:39:14,676 --> 00:39:17,366 only this script can use this variable, make this variable 964 00:39:17,366 --> 00:39:19,686 for all sprites, meant it was global. 965 00:39:19,686 --> 00:39:22,536 But thus far, we've only been looking at local variables, 966 00:39:22,536 --> 00:39:26,166 which A and B essentially are, because they're defined inside 967 00:39:26,166 --> 00:39:30,336 of this function swap and temp certainly is a local variable. 968 00:39:30,336 --> 00:39:32,616 So swap has three chunks of memory. 969 00:39:32,996 --> 00:39:37,276 It has A, it has B, and then it also has temp. 970 00:39:37,686 --> 00:39:41,046 And what happens the moment I call swap, as I said earlier, 971 00:39:41,086 --> 00:39:42,726 you get a copy of the inputs. 972 00:39:43,026 --> 00:39:44,796 Now they have different names. 973 00:39:44,796 --> 00:39:46,466 Not a problem because they're a different type. 974 00:39:46,466 --> 00:39:48,316 If they're the same types, that's what matters. 975 00:39:48,606 --> 00:39:53,056 So A gets 1, B gets 2, now the swap function itself starts 976 00:39:53,056 --> 00:39:55,946 executing line by line; so I declare a chunk 977 00:39:55,946 --> 00:39:56,856 of memory called temp. 978 00:39:56,856 --> 00:39:59,686 I then store in it A. This makes a copy. 979 00:39:59,896 --> 00:40:02,916 So the assignment operator thus far makes copies of things. 980 00:40:02,916 --> 00:40:04,016 It doesn't move the value. 981 00:40:04,246 --> 00:40:05,416 It just copies the bits. 982 00:40:05,636 --> 00:40:07,396 and if the bits represent one here, 983 00:40:07,396 --> 00:40:09,766 those exact same bits are going to represent one here. 984 00:40:10,156 --> 00:40:14,106 So now the second line of code is A gets B, semicolon. 985 00:40:14,376 --> 00:40:15,086 So what does that mean? 986 00:40:15,086 --> 00:40:18,776 Well, A has to get whatever is in B. So now I'm clobbering, 987 00:40:18,956 --> 00:40:21,316 as they say, this value here, and at this point 988 00:40:21,316 --> 00:40:23,086 in the story, this is not good. 989 00:40:23,086 --> 00:40:24,636 Right. Now I have two copies of two, 990 00:40:24,956 --> 00:40:26,506 but that's why I kept this thing around. 991 00:40:26,556 --> 00:40:28,796 B gets temp is the third line. 992 00:40:29,086 --> 00:40:32,616 So temp is here and B is here, that means 1 gets put here 993 00:40:33,186 --> 00:40:34,876 and now I print out the values. 994 00:40:35,036 --> 00:40:36,746 They are correct, as the picture suggests, 995 00:40:37,256 --> 00:40:38,566 but then I hit that curly brace. 996 00:40:38,906 --> 00:40:42,366 When I hit that curly brace, swap's role in life is over. 997 00:40:42,456 --> 00:40:43,876 There's no return statement. 998 00:40:43,976 --> 00:40:45,846 He's not actually returning any value. 999 00:40:45,846 --> 00:40:49,656 He's not returning A or B or temp, and definitely not X or Y; 1000 00:40:49,926 --> 00:40:53,086 so he just did all of this work and yet that's it. 1001 00:40:53,086 --> 00:40:53,696 What happens? 1002 00:40:53,736 --> 00:40:58,276 The moment swap returns, it says go, this never happens. 1003 00:40:58,656 --> 00:41:01,006 The computer essentially forgets about the memory 1004 00:41:01,006 --> 00:41:03,696 that was being used here, here, here for those three values. 1005 00:41:03,906 --> 00:41:05,986 You might very well have done some very accurate math 1006 00:41:06,396 --> 00:41:08,446 but it doesn't matter, because you did the math 1007 00:41:08,996 --> 00:41:09,916 in the wrong place. 1008 00:41:10,556 --> 00:41:13,466 So this issue of scope is to say that functions have scope. 1009 00:41:13,466 --> 00:41:16,316 And when you declare a variable, whether as an argument 1010 00:41:16,626 --> 00:41:18,276 or as literally a local variable, 1011 00:41:18,486 --> 00:41:20,886 they only live inside of that function. 1012 00:41:20,936 --> 00:41:23,616 The same deal in main. 1013 00:41:23,946 --> 00:41:26,826 It is not correct, for instance, for me to do this. 1014 00:41:26,896 --> 00:41:29,036 You know ,why am I wasting time passing things around. 1015 00:41:29,266 --> 00:41:31,436 Why don't I just do this and be clever. 1016 00:41:31,716 --> 00:41:36,446 Why don't I say int temp gets X. Let's say Y -- 1017 00:41:37,036 --> 00:41:41,716 oh, sorry, X gets Y and then Y gets temp, this 2 would fail. 1018 00:41:41,716 --> 00:41:42,836 It looks cleaner; right? 1019 00:41:42,836 --> 00:41:45,186 I'm not wasting time typing out arguments 1020 00:41:45,186 --> 00:41:47,156 and implementing more complicated function. 1021 00:41:47,206 --> 00:41:50,576 I'm simply saying swap X with Y, but this 2 won't work, 1022 00:41:50,616 --> 00:41:52,146 because where are X and Y defined? 1023 00:41:53,436 --> 00:41:54,786 In main. In a different scope. 1024 00:41:54,906 --> 00:41:56,136 They live in this memory. 1025 00:41:56,416 --> 00:41:58,956 Whereas, right now we only have access to this memory. 1026 00:41:58,986 --> 00:42:01,216 So this is a good thing and principle for security. 1027 00:42:01,216 --> 00:42:04,476 We're kind of restricting ourselves to only certain chunks 1028 00:42:04,476 --> 00:42:07,576 of memory, at least for now, but it certainly creating problems. 1029 00:42:07,576 --> 00:42:07,676 Yeah. 1030 00:42:08,516 --> 00:42:15,546 [ Inaudible ] 1031 00:42:16,046 --> 00:42:16,196 >> Indeed. 1032 00:42:16,196 --> 00:42:18,746 So there is a solution and that is the solution. 1033 00:42:18,746 --> 00:42:20,216 Shhh. But we will get there. 1034 00:42:20,516 --> 00:42:22,106 In fact, as we introduce arrays today, 1035 00:42:22,106 --> 00:42:24,476 we'll begin to see exactly how we can circumvent this 1036 00:42:24,646 --> 00:42:27,276 and what it's going to boil down to is moving away 1037 00:42:27,276 --> 00:42:30,296 from just using these user-friendly labels X, Y, Z, A, 1038 00:42:30,296 --> 00:42:32,366 B, and actually looking underneath the hood 1039 00:42:32,366 --> 00:42:34,546 at the numeric addresses, which we talked briefly 1040 00:42:34,546 --> 00:42:39,336 about in weeks past, at how this stuff is actually implemented 1041 00:42:39,336 --> 00:42:40,056 underneath the hood. 1042 00:42:40,296 --> 00:42:40,406 Yeah. 1043 00:42:41,516 --> 00:42:43,666 [ Inaudible ] 1044 00:42:44,166 --> 00:42:46,916 >> Initially, how come I don't have declare temp as what? 1045 00:42:47,016 --> 00:42:48,056 [ Inaudible ] 1046 00:42:48,056 --> 00:42:48,976 >> In which context? 1047 00:42:49,516 --> 00:42:52,546 [ Inaudible ] 1048 00:42:53,046 --> 00:42:54,456 >> Oh, oh, okay, good question. 1049 00:42:54,456 --> 00:42:56,666 So up top, the function prototype. 1050 00:42:57,056 --> 00:42:59,106 The only thing that you have to put in the function prototype, 1051 00:42:59,106 --> 00:43:00,976 again, is three things; one, the name of the function; 1052 00:43:01,336 --> 00:43:03,326 the return value; and its arguments. 1053 00:43:03,326 --> 00:43:06,156 You don't have to mention any of the local variables, 1054 00:43:06,156 --> 00:43:07,316 because at this point in the story, 1055 00:43:07,316 --> 00:43:08,446 they're just not relevant. 1056 00:43:08,446 --> 00:43:10,716 The only thing that compiler needs to know is kind 1057 00:43:10,716 --> 00:43:13,416 of what pattern to look for in the rest of your code. 1058 00:43:13,606 --> 00:43:17,436 And if it sees S-W-A-P, open parentheses, close parentheses, 1059 00:43:17,716 --> 00:43:19,546 and it hopefully sees two ints inside of it, 1060 00:43:19,756 --> 00:43:21,086 that's all it needs to do in order 1061 00:43:21,086 --> 00:43:22,926 to finish compiling your code. 1062 00:43:23,156 --> 00:43:25,146 All the local variables can be dealt with later. 1063 00:43:25,566 --> 00:43:25,986 Good question. 1064 00:43:26,246 --> 00:43:26,356 Yeah. 1065 00:43:26,746 --> 00:43:29,636 >> Why does it say void swap? 1066 00:43:29,636 --> 00:43:30,956 >> Why does it say void too often? 1067 00:43:30,956 --> 00:43:34,026 >> No. Void swap, like when you're doing a void swap, -- 1068 00:43:34,216 --> 00:43:35,666 >> So void, again, is my way 1069 00:43:35,666 --> 00:43:38,096 of saying swap does not return anything. 1070 00:43:38,096 --> 00:43:38,886 It doesn't return an int. 1071 00:43:38,886 --> 00:43:39,956 It doesn't return a float. 1072 00:43:40,356 --> 00:43:42,046 You have to with C, as with a lot 1073 00:43:42,046 --> 00:43:44,746 of programming language, say what you return. 1074 00:43:44,916 --> 00:43:46,896 And if you're returning nothing, you don't just leave it blank. 1075 00:43:47,106 --> 00:43:48,686 You say void and that's the convention. 1076 00:43:49,366 --> 00:43:49,476 Yeah. 1077 00:43:50,516 --> 00:43:54,546 [ Inaudible ] 1078 00:43:55,046 --> 00:43:55,556 >> Sorry, what about code? 1079 00:43:55,556 --> 00:43:59,696 >> Are scopes defined by curly braces or by functions? 1080 00:43:59,696 --> 00:44:00,216 >> Good question. 1081 00:44:00,216 --> 00:44:03,196 Are -- is scope defined by curly braces or by functions? 1082 00:44:03,196 --> 00:44:04,636 Absolutely, by functions, 1083 00:44:04,846 --> 00:44:07,986 but you can also create local scope with curly braces. 1084 00:44:08,226 --> 00:44:10,766 And so what I was hinting at earlier when you put semicolons 1085 00:44:10,766 --> 00:44:13,856 in the wrong place, and then proceed to have curly braces 1086 00:44:13,856 --> 00:44:15,456 around arbitrary lines of code, 1087 00:44:15,676 --> 00:44:18,496 you're creating an even more local scope there, 1088 00:44:18,956 --> 00:44:21,916 which is generally not the right intended behavior. 1089 00:44:22,516 --> 00:44:25,596 [ Inaudible ] 1090 00:44:26,096 --> 00:44:27,416 >> It's a good rule of thumb, yes. 1091 00:44:27,416 --> 00:44:29,476 You can find ways around this. 1092 00:44:29,476 --> 00:44:33,586 But a good rule of thumb is if you declare a variable, int X, 1093 00:44:33,646 --> 00:44:37,086 string Y, float Z, inside a curly brace, 1094 00:44:37,166 --> 00:44:39,326 and another curly brace, you can only use 1095 00:44:39,326 --> 00:44:42,526 that variable inside those two curly braces, specifically. 1096 00:44:42,946 --> 00:44:44,326 That's actually a really good rule of thumb. 1097 00:44:44,506 --> 00:44:46,196 Well, things can be fixed 1098 00:44:46,546 --> 00:44:49,006 by a little trick called global variables. 1099 00:44:49,006 --> 00:44:51,596 It's not necessarily the best solution but it is one solution, 1100 00:44:51,736 --> 00:44:52,896 and it's perhaps necessary -- 1101 00:44:52,896 --> 00:44:55,336 and when we get to PHP and Web Programming toward the talent 1102 00:44:55,336 --> 00:44:58,586 of the semester, you will actually deliberately use what 1103 00:44:58,586 --> 00:45:01,746 we call global variables to get access to any forms 1104 00:45:01,816 --> 00:45:04,286 that the user submits, if you're actually implementing an 1105 00:45:04,286 --> 00:45:07,226 application, a search engine, a web-based tool, 1106 00:45:07,226 --> 00:45:09,356 a Facebook like tool that takes input from the user, 1107 00:45:09,576 --> 00:45:12,836 you'll be handed those data by variables like these. 1108 00:45:12,946 --> 00:45:14,886 So what can we do to solve this? 1109 00:45:14,886 --> 00:45:18,336 Well, let's go ahead and open up global dot c, 1110 00:45:18,696 --> 00:45:19,996 or you can follow along up here. 1111 00:45:20,436 --> 00:45:22,676 So now notice this is almost the same program. 1112 00:45:22,906 --> 00:45:24,186 I've got my function main. 1113 00:45:24,386 --> 00:45:26,546 I then print X's now such and such, 1114 00:45:26,746 --> 00:45:28,166 initializing dot, dot, dot. 1115 00:45:28,366 --> 00:45:30,906 I now assign X the value of 1. 1116 00:45:30,906 --> 00:45:34,206 But wait a minute, I've already written buggy code it seems. 1117 00:45:34,406 --> 00:45:36,116 What have I clearly omitted from main? 1118 00:45:37,156 --> 00:45:39,636 So there's no mention of int, and yet we've been saying 1119 00:45:39,636 --> 00:45:40,806 for the past couple of lectures, 1120 00:45:40,806 --> 00:45:42,166 that anytime you declare a variable, 1121 00:45:42,166 --> 00:45:44,056 you absolutely have to specify its type. 1122 00:45:44,106 --> 00:45:45,516 And here I am now cutting corners. 1123 00:45:45,766 --> 00:45:46,406 Well, not quite. 1124 00:45:46,516 --> 00:45:48,966 If you scroll back up on the printout or screen here, 1125 00:45:49,256 --> 00:45:53,086 you'll see that you can actually not only declare function's 1126 00:45:53,276 --> 00:45:55,446 prototypes, their general structure. 1127 00:45:55,666 --> 00:45:59,006 At the top of your file, you can also put variables there. 1128 00:45:59,226 --> 00:46:00,526 You can put a variable there, 1129 00:46:00,526 --> 00:46:04,016 and because it's not encapsulated in curly braces, 1130 00:46:04,016 --> 00:46:04,956 as we just discussed, 1131 00:46:05,316 --> 00:46:07,716 that essentially means it's accessible everywhere 1132 00:46:07,786 --> 00:46:08,456 in that file. 1133 00:46:08,456 --> 00:46:12,916 Any function in this file now including main, swap, increment, 1134 00:46:13,006 --> 00:46:15,736 cube, whatever I want to implement, now has access 1135 00:46:15,776 --> 00:46:17,896 to this variable X. Now that can be a bad thing. 1136 00:46:18,126 --> 00:46:21,306 And you can very quickly take this feature to an extreme 1137 00:46:21,306 --> 00:46:23,896 and start putting X and Y and A and B and 10 1138 00:46:23,896 --> 00:46:25,836 and all your variables up top because it would seem 1139 00:46:25,836 --> 00:46:28,476 to solve all of your problems and stop all of your thinking, 1140 00:46:28,836 --> 00:46:30,446 but it's generally not a good thing. 1141 00:46:30,446 --> 00:46:33,316 Because when you start writing more compelling programs 1142 00:46:33,316 --> 00:46:35,616 and start using libraries, other people's code, 1143 00:46:35,616 --> 00:46:38,086 like the Google charts API's and things like this, 1144 00:46:38,316 --> 00:46:40,166 if you start putting all of your variables at the top 1145 00:46:40,166 --> 00:46:42,466 of your file and making them global, well, what happens 1146 00:46:42,466 --> 00:46:44,906 if you start integrating your code with other people's code, 1147 00:46:45,286 --> 00:46:47,476 things start to clash very quickly, and you have to go back 1148 00:46:47,506 --> 00:46:49,766 to the drawing board and fix all of the collisions, 1149 00:46:49,766 --> 00:46:52,386 all of the names that you happen to use, like X, Y, and Z, 1150 00:46:52,526 --> 00:46:54,576 and Google happened to use, like X, Y, and Z, 1151 00:46:54,816 --> 00:46:56,946 and so just very bad things happen quickly 1152 00:46:56,946 --> 00:46:59,256 and so you use globals sparingly, 1153 00:46:59,306 --> 00:47:00,306 but how do you use them. 1154 00:47:00,546 --> 00:47:02,556 Well, if you declare int X up top, 1155 00:47:02,556 --> 00:47:04,706 you could certainly update X to one here. 1156 00:47:04,996 --> 00:47:08,096 If I now scroll down to the actual implementation, 1157 00:47:08,576 --> 00:47:10,376 now does this work or not work? 1158 00:47:10,986 --> 00:47:15,346 So the hint is that this is our first program that's not been 1159 00:47:15,346 --> 00:47:16,356 called buggy something. 1160 00:47:17,136 --> 00:47:17,386 All right. 1161 00:47:17,716 --> 00:47:19,116 So it does, in fact, work. 1162 00:47:19,396 --> 00:47:22,876 And yet notice that the function itself, it takes no input. 1163 00:47:22,876 --> 00:47:25,216 I say, void to specify no input. 1164 00:47:25,216 --> 00:47:28,406 You can also get away, as I said last time, 1165 00:47:28,406 --> 00:47:30,556 with just putting parentheses in JavaScript and PHP, 1166 00:47:30,556 --> 00:47:31,986 this is, in fact, the norm. 1167 00:47:32,236 --> 00:47:35,516 In C, the truly correct way is to be very pathantic and say, 1168 00:47:35,726 --> 00:47:37,426 nothing is coming into this function; 1169 00:47:37,646 --> 00:47:39,156 so I'll explicitly say, void here. 1170 00:47:39,366 --> 00:47:42,246 So this function increment takes nothing, returns nothing, 1171 00:47:42,546 --> 00:47:43,936 and yet it does something. 1172 00:47:44,406 --> 00:47:47,016 It does, in fact, successfully increment X, 1173 00:47:47,446 --> 00:47:50,476 because at this point in the story, X was not scoped to main. 1174 00:47:50,676 --> 00:47:52,016 It was scoped to the whole file. 1175 00:47:52,016 --> 00:47:55,536 So pictorially, it's as though there's now some other part 1176 00:47:55,536 --> 00:47:58,036 of the blackboard here, maybe way up here 1177 00:47:58,036 --> 00:48:01,316 where I can put anything I want that's accessible to main, 1178 00:48:01,496 --> 00:48:04,426 that's accessible to swap, that's accessible to increment 1179 00:48:04,426 --> 00:48:06,106 or any other function I might implement 1180 00:48:06,406 --> 00:48:08,226 because it is, in fact, global. 1181 00:48:08,226 --> 00:48:10,636 Why don't we go ahead and take our five-minute break. 1182 00:48:11,516 --> 00:48:16,196 [ Pause ] 1183 00:48:16,696 --> 00:48:18,076 >> All right. 1184 00:48:18,186 --> 00:48:22,406 So I've got one more piece of bad code on this point 1185 00:48:22,476 --> 00:48:26,456 and this is buggy five dot c. So as a comment on top 1186 00:48:26,456 --> 00:48:29,486 of this file challenges, what is, in fact, the bug. 1187 00:48:29,486 --> 00:48:31,246 Well, let's take a look quickly together. 1188 00:48:31,246 --> 00:48:33,656 So I start this program by declaring 1189 00:48:33,656 --> 00:48:37,546 as I did before a global variable X. It is global simply 1190 00:48:37,546 --> 00:48:40,236 because it is not inside any of my functions. 1191 00:48:40,286 --> 00:48:42,876 It's instead outside of them and, by convention, 1192 00:48:42,876 --> 00:48:45,976 at the top of the file so that I can access it anywhere, whether 1193 00:48:45,976 --> 00:48:47,906 or not this is a good thing, remains to be seen. 1194 00:48:47,906 --> 00:48:49,636 But it is a solution to this problem. 1195 00:48:49,946 --> 00:48:52,486 I then declare increment early on. 1196 00:48:52,486 --> 00:48:55,376 My prototype, again, so that I can use it later 1197 00:48:55,546 --> 00:48:57,866 without the compiler wondering what is this increment 1198 00:48:57,866 --> 00:49:00,096 that you're referring to because it's not otherwise 1199 00:49:00,126 --> 00:49:00,786 been declared. 1200 00:49:00,786 --> 00:49:07,146 As an aside, why are you able to use print F without writing 1201 00:49:07,146 --> 00:49:08,356 out a prototype for it? 1202 00:49:09,646 --> 00:49:12,536 Well, remember this line; include standard IO dot H. 1203 00:49:12,776 --> 00:49:16,096 When I said in Week One that you don't actually have zeros 1204 00:49:16,096 --> 00:49:17,796 and ones inside this header file; right? 1205 00:49:17,796 --> 00:49:20,556 That's not actually where someone implemented print F 1206 00:49:20,556 --> 00:49:23,626 and related functions, rather, in dot H files, 1207 00:49:23,626 --> 00:49:26,016 by convention, are prototypes. 1208 00:49:26,546 --> 00:49:29,356 So this thing called a prototype generally lives -- 1209 00:49:29,356 --> 00:49:32,086 if someone else wrote code that other people are meant to use, 1210 00:49:32,346 --> 00:49:34,666 generally lives in these header files; whereas, 1211 00:49:34,666 --> 00:49:36,646 the implementation, the definition 1212 00:49:36,646 --> 00:49:38,906 of print F probably lives somewhere 1213 00:49:38,906 --> 00:49:41,466 in a corresponding dot C file. 1214 00:49:41,466 --> 00:49:43,336 So somewhere there's standard IO dot C, 1215 00:49:43,626 --> 00:49:46,956 somewhere they're standard IO dot H, but, for me, right now, 1216 00:49:47,156 --> 00:49:49,906 the only file I need to know about is the dot H, 1217 00:49:49,906 --> 00:49:51,906 because what Sharp include really does -- 1218 00:49:51,986 --> 00:49:53,956 it's what's called a preprocessor directive. 1219 00:49:54,286 --> 00:49:57,536 It pretty much means open this file, standard IO dot H 1220 00:49:57,576 --> 00:49:59,456 and just paste its contents right here. 1221 00:49:59,666 --> 00:50:02,646 Now because among its contents is the prototype for print F, 1222 00:50:02,646 --> 00:50:06,176 that is why, very simply, I can call print F without having 1223 00:50:06,246 --> 00:50:08,236 to declare its prototype myself. 1224 00:50:08,546 --> 00:50:10,216 With, thus far, we've taken that for granted. 1225 00:50:10,436 --> 00:50:12,256 Now my main function here is identical. 1226 00:50:12,456 --> 00:50:15,386 But here's a new and improved, I think, version of increment; 1227 00:50:15,666 --> 00:50:19,516 returns nothing, takes nothing, but it does perform plus plus, 1228 00:50:20,016 --> 00:50:21,826 but I did something stupid. 1229 00:50:21,826 --> 00:50:24,386 I did something that's allowed, but something stupid. 1230 00:50:24,656 --> 00:50:27,826 So here I'm declaring another variable called X, 1231 00:50:27,826 --> 00:50:30,406 and this is totally legitimate because I already know 1232 00:50:30,406 --> 00:50:33,826 that if I'm implementing a function like swap or increment, 1233 00:50:34,076 --> 00:50:36,396 I can absolutely take input. 1234 00:50:36,606 --> 00:50:37,496 I chose not to. 1235 00:50:37,656 --> 00:50:39,916 I can absolutely allocate local variables. 1236 00:50:40,036 --> 00:50:40,796 I chose to. 1237 00:50:41,046 --> 00:50:44,416 This time I called it X. I hard-coded into it the value 10. 1238 00:50:44,676 --> 00:50:46,866 There's still another variable 1239 00:50:46,866 --> 00:50:49,906 in this world called X. Its value is, by default, 1240 00:50:50,026 --> 00:50:54,286 what did we say one, and so now there's two copies of a variable 1241 00:50:54,586 --> 00:50:57,546 that -- there's two copies -- sorry, there's two variables 1242 00:50:57,696 --> 00:50:59,576 that happened to be named the same thing, 1243 00:50:59,846 --> 00:51:02,536 but just as I've drawn them physically in different places 1244 00:51:02,536 --> 00:51:05,476 on the board, if you think of this blackboard as a big chunk 1245 00:51:05,476 --> 00:51:08,516 of RAM, your 1 gigabyte of RAM, your 2 gigabytes of RAM 1246 00:51:08,516 --> 00:51:11,006 in your computer, this version of X is here. 1247 00:51:11,266 --> 00:51:12,766 This version of X is here. 1248 00:51:12,956 --> 00:51:15,186 And that's fine, because inside 1249 00:51:15,186 --> 00:51:18,306 of curly braces can you redeclare variables 1250 00:51:18,626 --> 00:51:21,256 with the same name if you intend 1251 00:51:21,256 --> 00:51:24,176 to quote unquote shadow the previous variable. 1252 00:51:24,486 --> 00:51:27,686 Now for now, its hard to argue that this is a good thing 1253 00:51:27,686 --> 00:51:28,756 but it's a common thing. 1254 00:51:28,926 --> 00:51:31,006 It's a common mistake to make if you forget 1255 00:51:31,006 --> 00:51:32,896 that I've actually declared that variable up top, 1256 00:51:33,016 --> 00:51:34,196 but wait a minute, I know any time 1257 00:51:34,196 --> 00:51:36,096 that I use a variable, I have to declare it. 1258 00:51:36,396 --> 00:51:40,166 Well, this will, in fact, be declared, but, unfortunately, 1259 00:51:40,386 --> 00:51:41,996 you're using a different chunk of memory. 1260 00:51:42,226 --> 00:51:43,876 You're incrementing this chunk of memory. 1261 00:51:43,936 --> 00:51:45,136 It's plus, plus to eleven. 1262 00:51:45,346 --> 00:51:48,126 You do the math correctly, increment returns. 1263 00:51:48,126 --> 00:51:50,186 What's the value of this X at the end of the day? 1264 00:51:50,886 --> 00:51:51,586 It's the same. 1265 00:51:51,586 --> 00:51:55,636 It's still 1, because you plus plussed the wrong symbol called 1266 00:51:56,236 --> 00:51:59,126 X. So for now, take-away from this, don't do this. 1267 00:51:59,346 --> 00:52:01,466 We'll see that there's actually compelling use cases 1268 00:52:01,636 --> 00:52:03,326 for using the same names of variables 1269 00:52:03,326 --> 00:52:04,186 in different context's. 1270 00:52:04,386 --> 00:52:05,996 Frankly, you'll see a matter of design 1271 00:52:05,996 --> 00:52:09,916 or style sometimes just makes sense to call the variable X 1272 00:52:10,446 --> 00:52:13,016 if that's consistent with the type of code you're writing 1273 00:52:13,016 --> 00:52:16,246 or maybe just to come up with an arbitrary example -- 1274 00:52:16,416 --> 00:52:18,896 maybe if you have to parameters and you want to call them left 1275 00:52:19,046 --> 00:52:20,986 and right because, conceptually, in your mind, 1276 00:52:20,986 --> 00:52:22,846 the purpose of this function is to do something 1277 00:52:22,846 --> 00:52:25,096 with the variable that's on the left-hand side in the world 1278 00:52:25,326 --> 00:52:27,146 and a variable that's on the right-hand side. 1279 00:52:27,356 --> 00:52:30,406 You don't want to rename them L and R or X and Y 1280 00:52:30,406 --> 00:52:31,856 because you want to call them left and right. 1281 00:52:32,076 --> 00:52:34,566 Well, you can but you have to appreciate the implications 1282 00:52:34,716 --> 00:52:36,576 of doing so is the take-away here. 1283 00:52:36,826 --> 00:52:38,706 So what's really going on inside of memory. 1284 00:52:38,706 --> 00:52:40,816 I've been using the blackboard a little ad hoc here, 1285 00:52:41,066 --> 00:52:43,416 but suppose I neaten things up here now and present 1286 00:52:43,456 --> 00:52:45,426 to you this rectangle that's going vertically, 1287 00:52:45,656 --> 00:52:47,856 this rectangle is let's say R RAM, 1288 00:52:48,046 --> 00:52:49,636 the memory inside of your computer. 1289 00:52:49,636 --> 00:52:52,116 This is different from the hard disk, which is where files 1290 00:52:52,116 --> 00:52:53,546 and stuff are permanently stored. 1291 00:52:53,766 --> 00:52:56,676 RAM is this memory recall or, as you'll soon find 1292 00:52:56,676 --> 00:52:59,756 in problems Set One, that's used for ephemeral purposes. 1293 00:52:59,856 --> 00:53:02,116 When you double-click a program, it's loaded into RAM. 1294 00:53:02,116 --> 00:53:05,856 When you're working on a file, it's saving constantly to RAM 1295 00:53:06,106 --> 00:53:08,346 but also hopefully to disc, the hard disk 1296 00:53:08,346 --> 00:53:10,146 so you don't actually lose it if the power goes off. 1297 00:53:10,446 --> 00:53:14,016 Well, if this is now my RAM, what's really happening is 1298 00:53:14,016 --> 00:53:16,326 that there is some organization to functions 1299 00:53:16,326 --> 00:53:19,986 in such being called, by default, if this is my RAM, 1300 00:53:20,426 --> 00:53:24,036 main excel can have parameters, as we'll see in just a moment. 1301 00:53:24,176 --> 00:53:27,196 Arg C and Arg V, those symbols I've been waving my hands at, 1302 00:53:27,416 --> 00:53:29,176 well where are they located in memory? 1303 00:53:29,406 --> 00:53:31,426 Well, memory is this rectangle, by default, 1304 00:53:31,426 --> 00:53:32,516 they're located at the bottom. 1305 00:53:32,726 --> 00:53:34,266 Now they may not take up the whole width. 1306 00:53:34,266 --> 00:53:35,976 This is somewhat of a artist's rendition, 1307 00:53:36,246 --> 00:53:38,736 but they're at the very bottom of my RAM. 1308 00:53:39,026 --> 00:53:40,026 Now what's on top of that? 1309 00:53:40,026 --> 00:53:43,756 Well, if main has any local variables X or Y or temp or foo 1310 00:53:43,756 --> 00:53:45,326 or bar, or whatever the case may be, 1311 00:53:45,566 --> 00:53:47,676 those variables come next in memory. 1312 00:53:47,746 --> 00:53:50,196 So you can think of it as left or right, top to bottom, 1313 00:53:50,196 --> 00:53:52,286 or whatever, the point is they come next in RAM. 1314 00:53:52,546 --> 00:53:56,306 Now if you call a function, like increment or cube or swap, 1315 00:53:56,306 --> 00:54:00,316 or in this case, foo, those variables are the parameters 1316 00:54:00,346 --> 00:54:03,366 to that function, end up getting stored next in memory. 1317 00:54:03,366 --> 00:54:07,186 So everything is literally going back to back to back and so foo, 1318 00:54:07,186 --> 00:54:09,266 similarly, if you have any local variables, 1319 00:54:09,266 --> 00:54:11,036 like temp, it then goes here. 1320 00:54:11,456 --> 00:54:14,556 So when I said earlier that as soon as swap returns, 1321 00:54:14,826 --> 00:54:17,316 all of the memory allocated for swap is now useless. 1322 00:54:17,316 --> 00:54:18,286 It just disappears. 1323 00:54:18,506 --> 00:54:20,296 That's because this process on roles. 1324 00:54:20,296 --> 00:54:23,626 If you think of these kind of like trays in a cafeteria, 1325 00:54:23,876 --> 00:54:25,836 right, that are kind of upside down, and by default, 1326 00:54:25,836 --> 00:54:28,066 they're stacked from the floor on up and up and up. 1327 00:54:28,126 --> 00:54:29,936 Well, if you want to call a function, 1328 00:54:30,106 --> 00:54:32,656 it's like putting another tray on that stack of trays 1329 00:54:32,846 --> 00:54:34,726 and that tray represents a chunk of memory that, 1330 00:54:34,726 --> 00:54:35,786 that function can use. 1331 00:54:35,886 --> 00:54:37,636 Well, if that function calls another function, 1332 00:54:37,636 --> 00:54:40,406 you put another tray on, and so that new tray represents 1333 00:54:40,406 --> 00:54:41,726 that function's chunk of memory. 1334 00:54:41,956 --> 00:54:44,716 But as soon as the most recently called function finishes 1335 00:54:44,716 --> 00:54:49,226 executing, you have to take that tray off the stack in order 1336 00:54:49,226 --> 00:54:51,126 to get at the previous function's memory, 1337 00:54:51,296 --> 00:54:53,646 and once he's done executing, you have to take that one off 1338 00:54:53,646 --> 00:54:55,756 and then what's left well then main. 1339 00:54:56,006 --> 00:54:58,136 So even though we're not literally throwing RAM away, 1340 00:54:58,136 --> 00:55:00,616 we're not physically moving anything, conceptually, 1341 00:55:00,786 --> 00:55:03,516 we have to go through memory in this order back to back 1342 00:55:03,546 --> 00:55:04,786 to back and then undo it. 1343 00:55:05,026 --> 00:55:07,676 So pictorially, you can imagine foo getting called 1344 00:55:07,676 --> 00:55:10,626 and then it finishes running; so these rows 1345 00:55:10,626 --> 00:55:12,476 in this chart will just be whisked away. 1346 00:55:12,626 --> 00:55:15,316 The bits are still there, much like bits are still left 1347 00:55:15,316 --> 00:55:17,266 when you erase a file on your hard disk 1348 00:55:17,616 --> 00:55:18,676 but they're forgotten about. 1349 00:55:18,766 --> 00:55:20,976 So pictorially, this row would simply disappear. 1350 00:55:21,226 --> 00:55:22,466 Now there's a bigger picture here. 1351 00:55:22,676 --> 00:55:25,246 I intentionally used this very common analogy of a stack 1352 00:55:25,246 --> 00:55:26,486 of trays in a cafeteria. 1353 00:55:26,876 --> 00:55:30,356 Well, because the memory we've been using for local variables, 1354 00:55:30,356 --> 00:55:33,826 and for functions, storage space is what computer scientists 1355 00:55:33,826 --> 00:55:34,866 generally call the stack. 1356 00:55:35,286 --> 00:55:37,536 It turns out that slightly before the stack, 1357 00:55:38,116 --> 00:55:40,096 slightly before this conceptual chunk of memory, 1358 00:55:40,306 --> 00:55:42,146 there's other things called environment variables 1359 00:55:42,146 --> 00:55:45,186 that we may see over time but elsewhere 1360 00:55:45,186 --> 00:55:46,736 in memory are other things. 1361 00:55:46,946 --> 00:55:49,026 Text is where the zeros and ones 1362 00:55:49,056 --> 00:55:51,556 that compose your file are actually stored. 1363 00:55:51,556 --> 00:55:54,736 So when you compile a program called A dot out, or skittles 1364 00:55:54,736 --> 00:55:56,976 or wherever, and you double-click that program, 1365 00:55:57,016 --> 00:55:58,576 or our in our command line environment, 1366 00:55:58,576 --> 00:56:01,756 run it with dot slash skittles, that program is loaded 1367 00:56:01,756 --> 00:56:03,366 into memory just like Microsoft Word 1368 00:56:03,366 --> 00:56:04,926 or whatever would be on your own computer. 1369 00:56:05,206 --> 00:56:06,386 Where is it put in memory? 1370 00:56:06,596 --> 00:56:09,376 Conceptually, it's put at the top of my chunk of RAM, 1371 00:56:09,826 --> 00:56:13,216 below it, goes initialized and uninitialized data -- 1372 00:56:13,216 --> 00:56:15,626 this is a fancy way of saying global variables come next -- 1373 00:56:15,936 --> 00:56:18,586 and below that, comes what's called the heap. 1374 00:56:18,846 --> 00:56:21,216 So before long, we'll see, especially for problems 1375 00:56:21,216 --> 00:56:23,686 that we can't allocate an int every time we want 1376 00:56:23,686 --> 00:56:25,006 to store something, because what if we want 1377 00:56:25,006 --> 00:56:26,926 to store 140,000 English words. 1378 00:56:26,926 --> 00:56:29,486 Right. We're not going to have 140,000 variables in my program. 1379 00:56:29,736 --> 00:56:31,596 I need access to more memory, and I need it fast. 1380 00:56:32,346 --> 00:56:34,156 Well, there's a chunk of memory called the heap 1381 00:56:34,406 --> 00:56:37,096 that you can grab as much memory as you want so long 1382 00:56:37,096 --> 00:56:39,166 as it exists for your program. 1383 00:56:39,486 --> 00:56:41,696 But there's kind of a problem here. 1384 00:56:41,696 --> 00:56:43,046 And this picture kind of makes clear 1385 00:56:43,046 --> 00:56:44,436 that we're headed for disaster. 1386 00:56:44,946 --> 00:56:46,786 Right. There's only a finite amount of memory. 1387 00:56:46,976 --> 00:56:49,596 And very deliberately has that memory been laid out as such. 1388 00:56:49,816 --> 00:56:50,566 So what's the problem? 1389 00:56:50,566 --> 00:56:52,736 We're probably going to run into very soon. 1390 00:56:53,286 --> 00:56:54,226 Right. Stack. 1391 00:56:55,566 --> 00:56:56,946 Stack. Stack. 1392 00:56:56,946 --> 00:56:58,806 You're calling functions, functions, functions, 1393 00:56:58,806 --> 00:57:01,086 but you're allocating heap, heap, heap, 1394 00:57:01,086 --> 00:57:02,276 heap memory for your words. 1395 00:57:02,276 --> 00:57:04,436 Eventually, stack and heap will collide. 1396 00:57:04,646 --> 00:57:07,426 And one of the ways you can make a program crash, intentionally 1397 00:57:07,426 --> 00:57:09,976 or not, is to essentially use up too much memory 1398 00:57:10,206 --> 00:57:13,246 or call too many functions and what happens is, bam, 1399 00:57:13,706 --> 00:57:15,936 one hits the other and bad things happen. 1400 00:57:15,936 --> 00:57:17,476 And, in fact, let's see -- 1401 00:57:17,546 --> 00:57:19,606 don't do this at home or on our systems -- 1402 00:57:20,026 --> 00:57:25,816 but let me go ahead and do nano don't do this dot C. Let's go 1403 00:57:26,116 --> 00:57:29,286 ahead and do int main void. 1404 00:57:30,336 --> 00:57:37,956 Let's go here and let's go ahead and say let's call this foo 1405 00:57:38,496 --> 00:57:42,826 and what I'm going to do here is void foo -- foo, incidentally, 1406 00:57:42,826 --> 00:57:44,366 is a stupid word the computer scientists use 1407 00:57:44,366 --> 00:57:45,436 when they just need a placeholder, 1408 00:57:45,516 --> 00:57:48,276 mathematicians say XYZ, we say foo, bar, Baz, clucks, 1409 00:57:48,276 --> 00:57:51,136 and then it goes on from there, you'll pick up on it. 1410 00:57:51,136 --> 00:57:53,796 Void foo. All right. 1411 00:57:54,276 --> 00:57:56,476 It's appropriate to make fun. 1412 00:57:56,736 --> 00:57:58,116 So what if I do this. 1413 00:57:58,306 --> 00:57:59,066 This is kind of trippy. 1414 00:57:59,856 --> 00:58:03,736 Right. So main calls foo, foo is declared, but wait a minute, 1415 00:58:03,786 --> 00:58:06,896 I need my prototype; so void foo, void. 1416 00:58:06,896 --> 00:58:08,316 I just do a copy paste of that. 1417 00:58:08,426 --> 00:58:10,206 Now there's a whole lot of foo and a whole of void here, 1418 00:58:10,206 --> 00:58:12,346 but this is consistent with the lessons thus far today. 1419 00:58:12,696 --> 00:58:16,636 Main calls foo, foo calls foo. 1420 00:58:16,786 --> 00:58:17,856 This is a problem. 1421 00:58:17,856 --> 00:58:19,606 Right. Just conceptually, there's this kind 1422 00:58:19,606 --> 00:58:21,466 of a funky picture that never seems to end. 1423 00:58:21,466 --> 00:58:23,486 So let's see what actually happens here. 1424 00:58:23,486 --> 00:58:24,956 I don't even need standard IO 1425 00:58:24,956 --> 00:58:25,946 because I'm not printing anything. 1426 00:58:25,946 --> 00:58:28,226 I'm really just messing around with the computer. 1427 00:58:28,496 --> 00:58:30,236 Let me go ahead and make, don't do this. 1428 00:58:31,446 --> 00:58:32,136 Looks good. 1429 00:58:33,076 --> 00:58:34,356 Okay. Literally, don't do this. 1430 00:58:36,316 --> 00:58:36,996 Bad. All right. 1431 00:58:37,036 --> 00:58:37,986 So segmentation fault. 1432 00:58:37,986 --> 00:58:40,296 Some of you have actually had the privilege 1433 00:58:40,296 --> 00:58:42,426 of having these errors already, for different reasons. 1434 00:58:42,426 --> 00:58:44,226 You probably weren't calling foo ad nauseam here 1435 00:58:44,476 --> 00:58:48,606 but segmentation fault refers to segments of memory colliding. 1436 00:58:48,906 --> 00:58:51,736 Two pieces of memory actually touching each other 1437 00:58:51,736 --> 00:58:53,716 or you touching memory that you don't actually own, 1438 00:58:53,966 --> 00:58:55,526 in which case the computer doesn't really know what 1439 00:58:55,526 --> 00:58:56,426 to do and just, bam. 1440 00:58:56,616 --> 00:58:57,696 It terminates your program. 1441 00:58:57,696 --> 00:59:00,666 And what very often happens if you do an LS in your account, 1442 00:59:01,336 --> 00:59:03,876 to list your files, there's a whole lot of stuff in here 1443 00:59:03,876 --> 00:59:06,836 at the moment -- and I don't have it here 1444 00:59:06,836 --> 00:59:09,486 but what you'll often see -- I'll just fake this demo -- 1445 00:59:09,886 --> 00:59:11,116 is a file called core. 1446 00:59:11,116 --> 00:59:13,536 If you ever see a file called core or something similar, 1447 00:59:13,746 --> 00:59:16,396 that generally means you made a bug, and it's a pretty bad bug, 1448 00:59:16,566 --> 00:59:19,316 and inside of core is we'll eventually see the contents 1449 00:59:19,316 --> 00:59:21,436 or some of the contents of the computer's RAM 1450 00:59:21,696 --> 00:59:24,576 at the moment your code screwed up, which is actually useful, 1451 00:59:24,576 --> 00:59:27,296 because soon we'll start using a debugger, a diagnostic tool, 1452 00:59:27,476 --> 00:59:31,046 where you can kind of do like an autopsy of your own program 1453 00:59:31,236 --> 00:59:32,726 and see what was inside of it 1454 00:59:32,726 --> 00:59:35,016 at the time this error actually happened. 1455 00:59:35,476 --> 00:59:38,446 So now let's actually put some of these ideas to use 1456 00:59:38,646 --> 00:59:39,896 in a more familiar way. 1457 00:59:40,136 --> 00:59:42,886 So I've used this jargon before; command line arguments, 1458 00:59:42,886 --> 00:59:43,946 but what does that really mean? 1459 00:59:44,146 --> 00:59:46,966 Well, anytime you run a program at the command line, like nano, 1460 00:59:47,176 --> 00:59:50,226 you've almost always been putting one or more words 1461 00:59:50,426 --> 00:59:55,986 after that program's name; nano hello dot C, GCC hello dot C, 1462 00:59:55,986 --> 00:59:59,176 and any number of MKDIR for make directory, 1463 00:59:59,176 --> 01:00:01,516 and then peace at one or similar. 1464 01:00:01,806 --> 01:00:03,266 So all of these programs or commands 1465 01:00:03,266 --> 01:00:05,546 that we've been using thus far for the piece set in class, 1466 01:00:05,876 --> 01:00:08,656 take what we call command line arguments, they are the zero 1467 01:00:08,656 --> 01:00:11,286 or more words that follow the program's name. 1468 01:00:11,466 --> 01:00:13,436 Well, thus far, I haven't written any programs 1469 01:00:13,436 --> 01:00:17,006 that themselves take any such arguments, and yet it turns 1470 01:00:17,006 --> 01:00:19,356 out I can do exactly that. 1471 01:00:19,746 --> 01:00:23,546 If I don't write the shorthand notation int, main, void, 1472 01:00:23,796 --> 01:00:25,776 but it explicitly says, you know what, 1473 01:00:25,776 --> 01:00:27,656 my version of main is actually going 1474 01:00:27,656 --> 01:00:29,606 to take some command line arguments, 1475 01:00:29,606 --> 01:00:33,706 and I want to call them by convention Arg C, and Arg V, 1476 01:00:33,996 --> 01:00:37,506 I can actually access input from the user without using get int, 1477 01:00:37,506 --> 01:00:39,676 without using get float, without using get string; 1478 01:00:39,886 --> 01:00:40,936 I can get it directly 1479 01:00:40,936 --> 01:00:43,566 at the blinking prompt, the command line. 1480 01:00:43,836 --> 01:00:46,876 So here's a program called Arg V, this is fancy speak 1481 01:00:46,916 --> 01:00:50,346 for argument Vector, a list of arguments for the left or right. 1482 01:00:50,826 --> 01:00:53,386 I have declared main as, int main, as before. 1483 01:00:53,596 --> 01:00:55,506 Now finally, I'm going to do something 1484 01:00:55,506 --> 01:00:56,726 with this crazy syntax. 1485 01:00:56,976 --> 01:00:59,816 Inside the curly braces is this program's code, what am I doing? 1486 01:00:59,996 --> 01:01:01,696 I'm actually just going to do something simple first, 1487 01:01:01,806 --> 01:01:03,516 print the arguments to this function. 1488 01:01:03,866 --> 01:01:07,296 I print F, I backslash N, just to pretty up the screen, 1489 01:01:07,586 --> 01:01:10,626 I then iterate from, I equal zero up to Arg C; 1490 01:01:10,916 --> 01:01:12,506 so Arg C is the convention. 1491 01:01:12,506 --> 01:01:14,726 You can call it foo if you want, but you shouldn't. 1492 01:01:14,966 --> 01:01:18,016 By convention, it's Arg C, because it means argument count. 1493 01:01:18,486 --> 01:01:22,126 How many words were typed at the prompt, by default, Arg C, 1494 01:01:22,126 --> 01:01:23,726 should always be at least one. 1495 01:01:23,996 --> 01:01:25,846 Because even though it's a little confusing 1496 01:01:26,156 --> 01:01:28,206 or a little sort of contradictory, 1497 01:01:28,846 --> 01:01:32,126 the argument count includes the name of the program itself, 1498 01:01:32,306 --> 01:01:34,406 which can actually be int useful because if someone -- 1499 01:01:34,406 --> 01:01:37,166 if a producer changes the name of a program by renaming a file, 1500 01:01:37,406 --> 01:01:38,926 you, the program still have access 1501 01:01:38,926 --> 01:01:41,826 to your own name by this mechanism. 1502 01:01:42,156 --> 01:01:42,806 Now what's this. 1503 01:01:43,136 --> 01:01:45,566 Well, a while back I said that we'll use it at least 1504 01:01:45,566 --> 01:01:47,666 for a few weeks this moniker string. 1505 01:01:47,926 --> 01:01:49,156 A string is a data type. 1506 01:01:49,196 --> 01:01:52,156 Doesn't really exist in C. It only exists in CS50's library, 1507 01:01:52,456 --> 01:01:55,646 but I then revealed that this is actually the word string is 1508 01:01:55,646 --> 01:01:56,716 really just a synonym 1509 01:01:56,716 --> 01:02:00,136 for a slightly more interesting syntax of char star. 1510 01:02:00,396 --> 01:02:03,336 A star is a preview, is referring 1511 01:02:03,336 --> 01:02:05,776 to what's called a pointer, similar in spirit 1512 01:02:05,776 --> 01:02:08,256 but more dangerous than references in Java 1513 01:02:08,256 --> 01:02:11,226 for those familiar, but we'll soon focus on these next week. 1514 01:02:11,486 --> 01:02:13,936 This really is just a way of saying this is a string. 1515 01:02:14,226 --> 01:02:15,896 The square brackets here, meanwhile, 1516 01:02:15,896 --> 01:02:17,706 say this is a special type 1517 01:02:17,706 --> 01:02:21,046 of variable that's got multiple values inside of it. 1518 01:02:21,336 --> 01:02:24,216 So thus far, when we've defined -- declared an int and a float, 1519 01:02:24,216 --> 01:02:26,896 and so forth, we just put one value in there, the number 2, 1520 01:02:27,086 --> 01:02:29,806 the number 3.14, just one single value. 1521 01:02:29,806 --> 01:02:32,116 But we said earlier that, you know, we're going to get 1522 01:02:32,116 --> 01:02:35,126 to the point where we want to have a lot of pieces of -- 1523 01:02:35,166 --> 01:02:38,766 a lot of variables in memory, maybe 140,000 dictionary words 1524 01:02:38,766 --> 01:02:41,576 for some future peace set, but we don't want to enumerate line 1525 01:02:41,576 --> 01:02:43,736 by line by line 140,000 variables. 1526 01:02:43,736 --> 01:02:46,836 It turns out you can list one, give it a name, 1527 01:02:46,836 --> 01:02:48,096 and if you use square brackets, 1528 01:02:48,146 --> 01:02:50,386 that tells the compiler I actually want 1529 01:02:50,386 --> 01:02:55,006 to put multiple values inside of this -- inside of this variable 1530 01:02:55,006 --> 01:02:56,166 and how do we do that? 1531 01:02:56,346 --> 01:02:57,216 Well, we'll see right now. 1532 01:02:57,606 --> 01:02:59,166 So this four loop is pretty familiar. 1533 01:02:59,216 --> 01:03:00,976 Arg C is just an int; so this line 1534 01:03:00,976 --> 01:03:03,156 of code here should be pretty comfortable by now. 1535 01:03:03,196 --> 01:03:04,526 Just iterates from zero 1536 01:03:04,526 --> 01:03:08,306 to Arg C. The indented line here print F is clearly printing a 1537 01:03:08,306 --> 01:03:11,526 string, as per the percent S, but what is it printing? 1538 01:03:11,846 --> 01:03:14,456 Well, the bracket notation means you can index 1539 01:03:14,666 --> 01:03:15,756 into this variable. 1540 01:03:16,076 --> 01:03:18,656 You can think of this variable Arg V 1541 01:03:18,886 --> 01:03:23,386 as literally an array a sequence of chunks of memory 1542 01:03:23,476 --> 01:03:25,986 that literally are back to back to back in memory, 1543 01:03:26,236 --> 01:03:29,916 and when you say bracket zero, by convention, you are referring 1544 01:03:29,916 --> 01:03:31,306 to the variables stored here. 1545 01:03:31,336 --> 01:03:33,536 When you say bracket one, you are storing -- 1546 01:03:33,536 --> 01:03:35,506 you are referring to the variable here, bracket two. 1547 01:03:35,996 --> 01:03:37,896 And how about if the array is size N, 1548 01:03:38,146 --> 01:03:40,806 and I say bracket N, where am I referring? 1549 01:03:41,016 --> 01:03:42,496 [ Inaudible ] 1550 01:03:42,496 --> 01:03:42,986 >> Too far. 1551 01:03:43,256 --> 01:03:45,456 Right. N is over here for counting from zero. 1552 01:03:45,456 --> 01:03:47,216 So this 2 is going to be a common mistake. 1553 01:03:47,456 --> 01:03:50,656 Last time I alluded to the exploit in the game Zelda 1554 01:03:50,656 --> 01:03:52,726 for the Wii, where people were taking advantage of this 1555 01:03:52,726 --> 01:03:54,096 and compromising the Wii system 1556 01:03:54,096 --> 01:03:55,296 and installing their own software. 1557 01:03:55,566 --> 01:03:58,226 It pretty much boils down to something simple as this. 1558 01:03:58,226 --> 01:03:59,896 That game uses an array for something, 1559 01:03:59,896 --> 01:04:01,926 maybe Zelda's inventory or something like that, 1560 01:04:02,266 --> 01:04:04,626 and the programmer who wrote the relevant line of code, 1561 01:04:04,946 --> 01:04:08,506 didn't check that the other functions are allowed -- 1562 01:04:08,816 --> 01:04:13,096 didn't check how far into this array other code is allowed 1563 01:04:13,096 --> 01:04:14,636 to index into; so somewhere 1564 01:04:14,636 --> 01:04:17,196 in Zelda is probably square bracket notation 1565 01:04:17,406 --> 01:04:22,196 where it's bracket N, instead of a maximum of N minus 1. 1566 01:04:22,506 --> 01:04:23,736 So when we count, generally, again, 1567 01:04:23,736 --> 01:04:25,876 we start from zero, we go to N minus 1. 1568 01:04:26,076 --> 01:04:28,586 If you ever try to go beyond the boundaries of an array, 1569 01:04:28,586 --> 01:04:30,576 as we'll soon see, bad things happen. 1570 01:04:30,726 --> 01:04:31,996 So now what are we doing here? 1571 01:04:32,236 --> 01:04:34,156 I'm first printing the zero argument 1572 01:04:34,156 --> 01:04:35,946 which I said is the name of the program itself. 1573 01:04:36,056 --> 01:04:38,786 I'm then printing bracket one, bracket two, bracket three, 1574 01:04:38,786 --> 01:04:42,126 bracket dot, dot, dot up until the total number of arguments, 1575 01:04:42,126 --> 01:04:43,936 whatever that may be, and it's going to be at least one 1576 01:04:43,936 --> 01:04:45,326 because the program always has a name. 1577 01:04:45,766 --> 01:04:47,476 So let's go ahead and compile this. 1578 01:04:47,476 --> 01:04:48,096 Make Arg V1. 1579 01:04:48,096 --> 01:04:50,376 It seems to run okay. 1580 01:04:50,646 --> 01:04:52,496 Now let's run Arg V1; enter. 1581 01:04:53,716 --> 01:04:54,906 Not all that interesting but there's 1582 01:04:54,906 --> 01:04:56,016 that new line I promised. 1583 01:04:56,016 --> 01:04:58,296 There's the name of the program including the dot slash. 1584 01:04:58,296 --> 01:05:01,136 Well, let's make this more interesting, foo. 1585 01:05:01,136 --> 01:05:02,176 All right. 1586 01:05:02,246 --> 01:05:03,216 Slightly more interesting. 1587 01:05:03,216 --> 01:05:05,076 Bar, slightly more interesting. 1588 01:05:05,076 --> 01:05:07,786 Baz, now it's going to keep doing this ad nauseam 1589 01:05:07,786 --> 01:05:09,256 until I hit some kind of limit. 1590 01:05:09,436 --> 01:05:11,326 But for now, it will pretty much go up forever. 1591 01:05:11,536 --> 01:05:13,086 But bad things will start to happen 1592 01:05:13,086 --> 01:05:17,556 if I don't mind this lesson of indexing. 1593 01:05:17,556 --> 01:05:19,246 If I do this, which is kind 1594 01:05:19,246 --> 01:05:21,436 of a mistake we made last time, if deliberately. 1595 01:05:21,436 --> 01:05:23,006 Let me re-save this. 1596 01:05:23,236 --> 01:05:27,806 Recompile my program, and now rerun Arg V of foo and bar. 1597 01:05:28,896 --> 01:05:31,056 Okay. So that's already some badness happening. 1598 01:05:31,056 --> 01:05:32,076 So why is that happening? 1599 01:05:32,256 --> 01:05:34,526 Well, again, just as I said verbally a moment ago, 1600 01:05:34,706 --> 01:05:36,956 if you're going here, you're literally touching, 1601 01:05:36,956 --> 01:05:40,706 trying to read or change memory that's beyond the boundaries 1602 01:05:40,776 --> 01:05:42,216 of a chunk of memory that you're supposed 1603 01:05:42,216 --> 01:05:43,926 to be touching based on its length. 1604 01:05:43,986 --> 01:05:47,416 And in this context, the length of that array is stored 1605 01:05:47,416 --> 01:05:50,306 in Arg C. Well, let's take a look at a slight variance 1606 01:05:50,306 --> 01:05:52,746 of this that reveals further what we can do 1607 01:05:53,056 --> 01:05:54,986 and reveals what a string really is. 1608 01:05:55,226 --> 01:05:56,386 So here's a program set 1609 01:05:56,386 --> 01:05:57,976 up in the same way, main looks the same. 1610 01:05:58,006 --> 01:05:59,676 I'm again, saying print argument's new line. 1611 01:05:59,846 --> 01:06:02,356 I'm again, iterating with this four loop, same code as before. 1612 01:06:02,356 --> 01:06:04,706 The only interesting thing now is a nested loop. 1613 01:06:04,966 --> 01:06:07,286 So when scratch blocked terms, I'd have a four loop 1614 01:06:07,286 --> 01:06:09,246 or like a forever loop or something 1615 01:06:09,246 --> 01:06:10,386 and then another one inside. 1616 01:06:10,736 --> 01:06:11,446 Same deal here. 1617 01:06:11,446 --> 01:06:12,696 The curly braces ensure 1618 01:06:12,696 --> 01:06:16,326 that everything is logically clustered where it should be. 1619 01:06:16,576 --> 01:06:17,316 Now what am I doing? 1620 01:06:17,516 --> 01:06:21,866 I'm iterating on the outside from I to Arg C, zero to Arg C; 1621 01:06:22,096 --> 01:06:24,076 on the inside, I better not use I; 1622 01:06:24,076 --> 01:06:25,466 otherwise bad things are going to happen. 1623 01:06:25,466 --> 01:06:28,096 If I'm iterating from I on the outside, I on the inside, 1624 01:06:28,096 --> 01:06:29,586 I'm going to start changing the value 1625 01:06:29,586 --> 01:06:32,176 and I might very well induce an infinite loop but J is okay. 1626 01:06:32,676 --> 01:06:33,656 So notice what you can do. 1627 01:06:33,656 --> 01:06:35,026 We've not seen this trick before, 1628 01:06:35,026 --> 01:06:36,846 at least, not in lecture. 1629 01:06:37,226 --> 01:06:40,126 For int, J gets zero, we can do that. 1630 01:06:40,126 --> 01:06:43,526 Thus far, we always put what after the zero? 1631 01:06:43,696 --> 01:06:46,826 In every example thus far we've had a semicolon saying that's 1632 01:06:46,826 --> 01:06:48,116 our initialization. 1633 01:06:48,296 --> 01:06:50,516 You can actually initialize multiple things at once 1634 01:06:50,556 --> 01:06:53,096 by separating them with a comma, and this is sometimes helpful. 1635 01:06:53,336 --> 01:06:55,216 It turns out there's a new function 1636 01:06:55,216 --> 01:06:58,766 that you probably have not used yet called strlen, S-T-R-L-E-N, 1637 01:06:59,026 --> 01:07:02,006 programmers early on and still like to be succinct 1638 01:07:02,006 --> 01:07:04,176 but communicates sufficiently what they mean. 1639 01:07:04,376 --> 01:07:06,196 So strlen is the length of a string. 1640 01:07:06,506 --> 01:07:09,556 Well, Arg V bracket 1, is, in fact, a string. 1641 01:07:09,736 --> 01:07:15,066 Because I've said that char Star is really just the true version 1642 01:07:15,186 --> 01:07:15,616 of this. 1643 01:07:15,816 --> 01:07:18,346 So conceptually, what is main being handed? 1644 01:07:18,346 --> 01:07:20,416 It's being handed an array of strings. 1645 01:07:20,416 --> 01:07:23,376 It's being handed a picture that looks like this, and inside each 1646 01:07:23,376 --> 01:07:26,786 of these squares is a word foo and bar and baz as well 1647 01:07:26,786 --> 01:07:27,756 as the program's name. 1648 01:07:28,086 --> 01:07:30,976 But here, I'm just being a little more pedantic, 1649 01:07:30,976 --> 01:07:32,406 that's really what a string is. 1650 01:07:32,496 --> 01:07:33,696 We'll see why before long. 1651 01:07:33,966 --> 01:07:38,616 So if Arg V just contains a whole bunch of strings, surely, 1652 01:07:38,616 --> 01:07:40,816 using this new square bracket notation, 1653 01:07:40,816 --> 01:07:44,136 can I go to this string or this string or zero or one or two 1654 01:07:44,136 --> 01:07:46,456 or three, but you know what, I want to know the length 1655 01:07:46,456 --> 01:07:48,366 of each those strings, thankfully, 1656 01:07:48,406 --> 01:07:50,576 someone else wrote a function called strlen, 1657 01:07:50,786 --> 01:07:54,586 that's given a string, given a bunch of squares like this, 1658 01:07:54,956 --> 01:07:58,566 whereby, each of them contains -- given a string -- sorry -- 1659 01:07:58,566 --> 01:08:01,656 let me retract that, given a string, Arg V bracket one, 1660 01:08:01,686 --> 01:08:03,246 it tells you how many characters are in it. 1661 01:08:03,246 --> 01:08:05,596 If I type foo, I'll get back F-O-O. 1662 01:08:05,776 --> 01:08:10,326 Now here's the semicolon, J less than N, where N is this, J plus, 1663 01:08:10,326 --> 01:08:11,416 plus; so what am I doing? 1664 01:08:11,646 --> 01:08:12,976 Well, turns out that with arrays, 1665 01:08:13,156 --> 01:08:14,666 not to make this too trippy too soon, 1666 01:08:14,926 --> 01:08:17,966 but Arg V bracket something is a string, 1667 01:08:18,406 --> 01:08:20,576 you can also use square bracket notation to go 1668 01:08:20,576 --> 01:08:22,446 to a specific letter in a string, 1669 01:08:22,676 --> 01:08:25,356 because little teaser a string is just an array. 1670 01:08:25,356 --> 01:08:28,726 When you've been typing in words to the keyboard like your name 1671 01:08:29,056 --> 01:08:31,946 or any word that you're trying to provide to the user, 1672 01:08:32,146 --> 01:08:33,846 a string really looks like this. 1673 01:08:33,936 --> 01:08:36,656 Is it a character of eight bytes -- sorry -- 1674 01:08:36,716 --> 01:08:38,856 eight bits, eight bits, eight bits, 1675 01:08:38,856 --> 01:08:40,426 and I've done the same picture on the board. 1676 01:08:40,666 --> 01:08:44,356 So if a string is actually implemented as an array 1677 01:08:44,356 --> 01:08:49,086 of characters, much like Arg V itself or arrays in general, 1678 01:08:49,326 --> 01:08:54,576 are just implemented as array of strings, well, so can I iterate 1679 01:08:55,066 --> 01:08:57,566 over each of the arguments as I did a moment ago, 1680 01:08:57,566 --> 01:08:59,846 but then also each of the characters 1681 01:09:00,106 --> 01:09:01,096 in each of the strings. 1682 01:09:01,726 --> 01:09:06,536 So Arg V bracket I, gives me the Ife string that I typed in. 1683 01:09:07,026 --> 01:09:12,126 Arg V bracket I bracket J, gives me the J character 1684 01:09:12,216 --> 01:09:14,586 or letter in the Ife word. 1685 01:09:14,806 --> 01:09:17,616 So that's all we're doing, picking a word and we're homing 1686 01:09:17,616 --> 01:09:19,356 in on a specific character. 1687 01:09:19,356 --> 01:09:20,366 So what's the end result. 1688 01:09:20,506 --> 01:09:23,236 Well, notice I'm calling print F with percent C, 1689 01:09:23,456 --> 01:09:26,116 and then I'm printing out each of these letters one at a time. 1690 01:09:26,676 --> 01:09:27,726 Let's go ahead and make this. 1691 01:09:27,796 --> 01:09:29,226 So this is make Arg V2. 1692 01:09:29,226 --> 01:09:33,606 Let me go ahead and now run Arg V2 with nothing. 1693 01:09:34,106 --> 01:09:35,646 Well it's a stupid looking program, 1694 01:09:35,816 --> 01:09:37,656 but because I had more new line characters, 1695 01:09:37,656 --> 01:09:40,216 and because I was using percent C, for individual characters, 1696 01:09:40,216 --> 01:09:45,156 one per line, now I'm seeing dot slash ARG V2, well, 1697 01:09:45,156 --> 01:09:47,386 now if I go ahead and rerun this with foo, 1698 01:09:47,566 --> 01:09:49,196 I get this effect as well. 1699 01:09:49,646 --> 01:09:52,506 So for now, a string is really just an array. 1700 01:09:52,606 --> 01:09:54,876 It's a picture that, again, is literally like this. 1701 01:09:55,016 --> 01:09:57,086 Somewhere in memory, back to back to back or all 1702 01:09:57,086 --> 01:10:00,686 of the strings and with them can we store actual words 1703 01:10:00,686 --> 01:10:01,316 and phrases. 1704 01:10:01,606 --> 01:10:03,536 Now why is this actually useful? 1705 01:10:03,966 --> 01:10:07,866 So now that we begin to have this ability to express strings 1706 01:10:07,866 --> 01:10:09,876 in memory, to understand what they are, we can start 1707 01:10:09,876 --> 01:10:12,476 to apply actually real-world algorithms to them. 1708 01:10:12,476 --> 01:10:17,576 So does anyone know or recall what this says? 1709 01:10:17,796 --> 01:10:22,916 This is a sentence or string phrase that has been encrypted. 1710 01:10:22,916 --> 01:10:25,966 It's been encrypted pretty naively, pretty simplistically, 1711 01:10:26,366 --> 01:10:28,396 but it's actually an English sentence that's been somehow 1712 01:10:28,396 --> 01:10:30,696 scrambled; but scrambled in a reversible way. 1713 01:10:30,696 --> 01:10:32,556 If you just scramble a sentence, it's pretty useless 1714 01:10:32,606 --> 01:10:34,316 because the recipients are never going to be able 1715 01:10:34,316 --> 01:10:35,216 to reverse the process. 1716 01:10:35,716 --> 01:10:36,556 So there's a pattern here. 1717 01:10:37,256 --> 01:10:42,636 Think back to a movie's that's on TBS, ad nauseam, 1718 01:10:42,636 --> 01:10:44,576 in December called Christmas Story. 1719 01:10:45,476 --> 01:10:46,976 Anyone? Little Ralphie. 1720 01:10:47,066 --> 01:10:49,066 [ Inaudible ] 1721 01:10:49,116 --> 01:10:50,536 >> Yes. Be sure that's one of two of us have seen this movie. 1722 01:10:50,536 --> 01:10:51,926 Be sure to drink your Ovaltine. 1723 01:10:51,926 --> 01:10:53,476 So Google it or look 1724 01:10:53,476 --> 01:10:54,906 at the Wikipedia article for this movie. 1725 01:10:55,096 --> 01:10:57,806 But in this movie, the kid gets a little secret decoder ring. 1726 01:10:58,006 --> 01:10:59,386 It looks a little something like that, 1727 01:10:59,496 --> 01:11:01,736 and the screen doesn't quite do it justice here, 1728 01:11:01,956 --> 01:11:05,186 but there's two alphabets; A through Z, and then A through Z, 1729 01:11:05,296 --> 01:11:07,916 but the inner A through Z is on a small -- 1730 01:11:07,916 --> 01:11:10,456 it has a smaller diameter; so it's a ring of letters. 1731 01:11:10,736 --> 01:11:13,366 Outside of that are the same letters but slightly wider 1732 01:11:13,526 --> 01:11:14,886 and the whole thing roates. 1733 01:11:15,376 --> 01:11:17,946 So if you had A here, you can rotate the outer disk 1734 01:11:17,946 --> 01:11:19,976 so that the A lines up, not with A, 1735 01:11:19,976 --> 01:11:24,056 but B or C. So what you're seeing here is a very simple pop 1736 01:11:24,056 --> 01:11:26,946 culture reference to something generally known as a Caesar site 1737 01:11:26,946 --> 01:11:30,066 for a drive from Julius Caesar, who, purportedly, years ago, 1738 01:11:30,106 --> 01:11:32,516 used to send in encrypted messages using precisely this 1739 01:11:32,516 --> 01:11:34,586 fairly simplistic mechanism; whereby, 1740 01:11:34,586 --> 01:11:37,146 you take a message you want to write you then scramble it 1741 01:11:37,376 --> 01:11:41,286 by rotating conceptually every letter by probably more 1742 01:11:41,286 --> 01:11:44,576 than one place, but fewer in English than say 26 places, 1743 01:11:44,636 --> 01:11:45,926 lest you be doing no work at all. 1744 01:11:46,216 --> 01:11:48,606 You rotate the letters by a deterministic amount, 1745 01:11:48,606 --> 01:11:50,136 by a fixed number of steps 1746 01:11:50,416 --> 01:11:52,506 so that the end result is actually something 1747 01:11:52,506 --> 01:11:53,406 that can be reversed. 1748 01:11:53,606 --> 01:11:56,146 The number that you choose to rotate your letters 1749 01:11:56,146 --> 01:11:58,516 by is what we call in cryptography, a key. 1750 01:11:58,726 --> 01:12:00,206 It's a secret number so that 1751 01:12:00,206 --> 01:12:04,256 if Julius Caesar scrambled his message with the key 13, well, 1752 01:12:04,256 --> 01:12:06,266 then the recipient had better know what that number is 1753 01:12:06,686 --> 01:12:09,796 or realistically, -- and this is why it's kind of hard to buy 1754 01:12:09,796 --> 01:12:12,176 into some of these age old algorithms, -- 1755 01:12:12,446 --> 01:12:14,496 what would an alternative approach be to figuring 1756 01:12:14,496 --> 01:12:17,256 out Julius Caesar's messages to his generals if he didn't know 1757 01:12:17,256 --> 01:12:18,606 that secret number was 13? 1758 01:12:19,946 --> 01:12:21,006 So it's a trial and error. 1759 01:12:21,006 --> 01:12:23,046 You can brute force it and unfortunately, 1760 01:12:23,046 --> 01:12:25,476 the so-called Caesar cipher is not very secure 1761 01:12:25,476 --> 01:12:28,036 because if you assume, for our purposes in English alphabet 1762 01:12:28,216 --> 01:12:31,076 with 26 characters, say all lowercase for all uppercase, 1763 01:12:31,336 --> 01:12:34,026 my God, you only have to try like 24, 25, 1764 01:12:34,026 --> 01:12:37,296 26 possible rotations until you can figure 1765 01:12:37,296 --> 01:12:38,886 out what his secret message is. 1766 01:12:38,886 --> 01:12:40,596 So there are more sophisticated approaches, 1767 01:12:40,856 --> 01:12:41,786 some of them are mechanical. 1768 01:12:41,786 --> 01:12:44,706 Those of you with backgrounds in history or we've seen -- 1769 01:12:45,506 --> 01:12:49,526 or remember World War II with technologies that were used 1770 01:12:49,526 --> 01:12:51,266 in it might know of the Enigma machine, 1771 01:12:51,476 --> 01:12:52,716 which is kind of the same idea. 1772 01:12:52,716 --> 01:12:55,716 You type at the keyboard words that you want to encrypt. 1773 01:12:56,076 --> 01:12:59,156 So something the Germans used on their U-boats and the output 1774 01:12:59,156 --> 01:13:00,566 of that is a scrambled message. 1775 01:13:00,856 --> 01:13:05,966 Here, the secret is not a number 13, it's actually the build -- 1776 01:13:06,256 --> 01:13:10,366 the diagram, the details, the gears, and the handles inside 1777 01:13:10,366 --> 01:13:12,846 of this physical machine that constituted a secret. 1778 01:13:12,846 --> 01:13:15,006 So one of the things that the Allies very specifically wanted 1779 01:13:15,006 --> 01:13:18,446 to do in World War II was to hijack one of these devices, 1780 01:13:18,486 --> 01:13:21,816 take it from a U-boat because if they had this physical machine, 1781 01:13:22,146 --> 01:13:24,206 certainly can they start decrypting then the 1782 01:13:24,206 --> 01:13:24,986 German messages. 1783 01:13:24,986 --> 01:13:26,646 They don't have to know something in this case. 1784 01:13:26,886 --> 01:13:29,016 They have to have something in this case. 1785 01:13:29,286 --> 01:13:30,946 So in general, both of these are examples 1786 01:13:30,946 --> 01:13:32,886 of what we call secret key crypto. 1787 01:13:32,886 --> 01:13:35,216 This little picture here offer some jargon. 1788 01:13:35,216 --> 01:13:37,456 If you have some plain text, some message that you want 1789 01:13:37,526 --> 01:13:39,006 to send, well, you can encrypt it 1790 01:13:39,006 --> 01:13:41,786 or you can scramble it using a key, a secret key, 1791 01:13:41,786 --> 01:13:43,766 which might be a physical machine like the Enigma 1792 01:13:44,076 --> 01:13:46,056 or it might be a number like 13. 1793 01:13:46,276 --> 01:13:49,026 But the catch is that the party on the other end of things has 1794 01:13:49,256 --> 01:13:52,046 to have that same machine or know that same number. 1795 01:13:52,216 --> 01:13:53,536 But this is a huge problem. 1796 01:13:53,776 --> 01:13:55,786 Right. Because all of us have probably bought something 1797 01:13:55,786 --> 01:13:58,446 on Amazon.com or the like, all of us have probably logged 1798 01:13:58,446 --> 01:14:01,276 into Facebook.com, passing your credit card information 1799 01:14:01,276 --> 01:14:03,286 over the Internet, passing your username and password 1800 01:14:03,286 --> 01:14:05,106 over the Internet, and as you probably know, 1801 01:14:05,106 --> 01:14:08,576 as a casual user, https denotes secure. 1802 01:14:08,576 --> 01:14:10,316 There's cryptography going on. 1803 01:14:10,706 --> 01:14:13,426 But how many of you have ever called Facebook up 1804 01:14:13,426 --> 01:14:15,356 or called Amazon up and kind of whispered 1805 01:14:15,356 --> 01:14:18,446 into their ear I'm going to encrypt this with the number 13. 1806 01:14:18,916 --> 01:14:22,076 Right. So you have this -- it's kind of a stupid thing. 1807 01:14:22,076 --> 01:14:23,676 Right. If you're doing that, it's a little strange. 1808 01:14:23,976 --> 01:14:25,826 But the point is that how is Facebook, 1809 01:14:25,826 --> 01:14:29,106 how is Amazon possibly going to communicate securely with you 1810 01:14:29,106 --> 01:14:32,436 or your laptop using cryptology if you both have 1811 01:14:32,436 --> 01:14:33,556 to know the same secrets. 1812 01:14:33,906 --> 01:14:35,686 Well, this is a problem that we have solved 1813 01:14:35,686 --> 01:14:37,626 and will solve in the weeks to come. 1814 01:14:37,626 --> 01:14:37,976 See you Monday. 1815 01:14:38,516 --> 01:14:45,190 [ Silence ]