[ Silence ] >> [Sound effects] Be it known through all in summary that Ralph Parker is here by appointed a member of the Little Orphan Annie secret circle and is entitled to all the honors and benefits occurring thereto. >> Signed Little Orphan Annie countersigned Pierre Andre in ink. >> Honors and benefits already at the age of nine. [ Sound effects ] >> Come on let's get on with it. I don't need all that jazz about smugglers and pirates. [Sound effects] >> Listen tomorrow night for the concluding adventure of the Black Pirate Ship. Now it's time for Annie's secret message for you members of the secret circle. Remember kids, only members of Annie's secret circle can decode Annie's secret message. Remember Annie is depending on you. [Background sound effects] Set your pens to B2. Here is the message 12, 11 -- >> [Background sound effects] I attend my first secret meeting. >> 14, 11, 18,16 -- >> [Background sound effects] Pierre was in great voice tonight. I could tell that tonight's message was really important. >> [Background music] 3, 25. That's a message from Annie herself. Remember don't tell anyone. >> 90 seconds later I'm in the only room in the house where a boy of 9 could sit in privacy and decode. [Laughter] Ah hah, B and on to the next, E the first word is be. S, it was coming easier now. U >> All come on Ralphie. [Background sound effects] I'll be right down Ma, gee whiz. >> [Background music] T,O. Be sure to what? What was Little Orphan Annie trying to say? Be sure to what? [Background sound effects] Alright mom, I'll be right out. >> [Background music] I was getting closer now. The tension was terrible. What was it? The fate of the planet may hang in the balance. [Sound effects] >> I'll be right out for crying out loud. >> [Background music] Almost there. My fingers flew. My mind was a steel trap. Every pore vibrated. It was almost clear. Yes, yes, yes, yes. >> Be sure to drink your Ovaltine. Ovaltine. A crummy commercial. [Background music] Son of a bitch. [Laughter] >> This is CS50. Welcome to the start of week 2. So this excerpt from A Christmas Story, which is a movie that plays incessantly around Christmas time in this country, pretty much 24-7. Is actually an interesting example of cryptography or code. And this is one of the real world domains that we'll introduce this week for the sake of motivation not only for the material of the week but also for problems that too will be going into your hands later this week. So what little Ralphie had there was a secret decoder ring of sorts. Essentially it had the alphabet, A through Z on one side of that circle and then A through Z on another. And what the guy on the radio told them was how to line those two circles up so that A might map to M and B might map to another letter, so it was a code. And he was intent on actually decoding that. So what you saw was actually an instance of what geeks have known for some time is rot13, r o t 13. This is a rotational cipher and if you're increasingly geekified by courses such as this you will understand the irony of a quote like this. Let's see if you do. [ Sound effects ] >> So rot13 to be clear is when you take the alphabet and you take an input and you rotate all of the letters 13 places. [Laughter] There we go. If it's kind of one of these moments right now, just ask the person next to you. So this was-- this is sort of a age old joke but it's an example of something more generally known as a Caesar cipher. And a Caesar cipher's something we'll look at in more academic detail on Wednesday. But the joke here is of course, I'll leave it to the astute thinker there. So a couple of announcement -- one new handout for today the printout of the source code that we'll be using for the week. All of this stuff is also available online. And also if you weren't paying attention last week or weren't here, do realize that so that you are freed intellectually not to scribble down most anything that is said in class. Do realize that we've been putting up scribe notes thanks to one of the teaching fellows, which are an authoritative set of notes so to speak in PDF format on the website. And also know too with regard to problem sets, so problem set 1 went out on Friday. And from some of the e-mails we got we know some of you dove into it. But it's okay if normal people are waiting until later this week to start. But do realize per the syllabus that the course offers you a number of late days for problem sets. Nine specifically, which means you can give yourself a 24 hour extension for instance at any point in the semester simply by cashing in one of your late days. Once you know who your teaching fellow is we simply expect that you advise him or her that you're doing it, so they don't start grading and then realize that oh you're not actually in their pile. But you can spend one late day, nine late days, we would encourage you just from our own experience to be conservative up front because inevitably things come up later in the semester. But also to mitigate that inevitable Pset that you just completely screw up for whatever personal or academic reason, we will also drop your lowest Pset score at the end of the semester. So realize that support structure is there. Super sections are taking place today, tomorrow and Wednesday, check the courses website for the Google calendar that will show you when and where they are. So these are just sections but they're just a few sections that anyone and everyone can go to irrespective of comfort level. So if you are in -- among those less comfortable let's say, those in between these are apps specifically targeted at that demographic this week so that you feel that you do have a support structure in place for the first problem set. One of our teaching fellows Marta led the first walkthrough last night, it was filmed and that video will go online as well. And the walkthroughs are meant to be an authoritative how to on how to begin the Pset where sections will focus not only on some related piece of material but also on lecture material and the stuff you should be getting more comfortable with. Late mid-week will you find out via e-mail what your assigned section and teaching fellow is and in a class this size inevitably we need to shuffle people around, that's okay we'll fix any mistakes that we or your schedules have made. Office hours and virtual office hours have also begun. Office hours are an opportunity to drop down into the basement of the science center. You go down those stairs, you take a left there's some white boards and that's generally where the TFs and CAs hang out. If you've got a question you write your name on the board. You generally write down the number of the computer station that you're at, they're all numbered with stickers and then we the staff go through the list top to bottom, left to right. And then eventually right to left, bottom to top going back and forth through the list crossing names off and then plucking off people's questions so realize that that exists. And then also this little technology, so sometimes it just doesn't make all that much sense to go into -- say trek across campus to actually get to the science center to just ask like a 1 minute question. And sometimes e-mail's not perfect for it because sometimes you want the staff member to see what's on your screen, so we've offered for some time now what we call virtual office hours using a virtual terminal room in contrast with the science center's physical terminal room. And essentially we do this by a program called Elluminate. It's special software that's free for all of us to use, it's cross platform, works Windows, MACs, Linux computers. And essentially when you want to attend virtual office hours you'll click a button like this under VTR, you will then log in for now with you FAS user name and password. My connection's being a bit slow but once you do so, you will then be led hopefully after very few, if any technical hiccups to a window that's going to pop up. The first time you use it, it's going to install some special Java software and then even if it doesn't come up right now that's fine. You will essentially have a glorified chat room so there's going to be a little portion of the screen where you can type back and forth with the teaching staff. But more compellingly there is a white board so to speak, which takes up most of the screen and the neat thing with that is that the teaching fellows and the CAs can take control of or share control of your screen, your keyboard and mouse. And actually help you troubleshoot something that is on your screen at that moment and time. And if you're a little freaked out by that idea, realize we won't necessarily have the ability to see your Facebook windows, your personal chat windows and all of that. It's very much an opt in thing, it's not either or so I'll let that load in the background and pull it up at some point. But for now a couple of final notes. We will eventually have a bulletin board on the courses website once we have enrollment data and can actually authenticate people properly. But for now if you have any questions that are appropriately e-mailed just drop a note to cs50.net and do get into the habit of being as clear and as precise and as technical as possible. Sending us e-mails whatever the hour with comments like, hi I'm having some trouble with my code. Do you have any advice? Not really, you gotta be more specific than that. Include your source code, specify your problems. Tell us what you've done. What did work, did not work and so forth. Help us help you, because realize we get a lot of such e-mails. So we like to pluck off the ones that are really clear and really straight forward. Sensor boards, if you did hacker edition on your way out today we've got a couple of cardboard boxes, just drop them off. But on that slip of paper write your name, ID number and FAS user name so we can cross you off the list. And then finally, this. So Harvard, not so rich anymore but thanks to some wonderfully supportive alumni who have fond memories of CS50 and computer science here. We will be fortunately able to continue a little tradition we began last year, which was lunch with David and team or et al whereby we hang out with 10, 20, 30 students. Whoever's free on Friday afternoons. We go to lunch somewhere, sit down, chit chat, talk about the course. Talk about past experiences at Harvard, whatever the case may be and it's just a chance to make a big class a lot more intimate. So if you're interested in hanging out with me and some of the TFs and CAs over lunch this Friday, 1:15 p.m. just go to cs50.net/rsvp and we'll let you know where and all of the logistics there. So it's a nice tradition that thanks to some generous alumni we will begin, we will be able to continue this year. So I went back and did my homework. I re-downloaded our surveys and I made this integrated into the courses website using Google maps, something you'll be able to do later in the semester. It was actually a good sanity check because we've got a couple of notes from students saying, "Hi my country is not listed here, my city is not listed here. Did I actually set my problem set correctly?" Sometimes the answer was yes, sometimes the answer was no, so it was actually a nice little way of checking as much. But this is again just a mash up so to speak of all of the interesting data that we collected. So more on that in the future. Do take notes if you haven't already then on the problem sets page there has been for a week, galleries not only from the past two years but from this past year. So it's one thing to actually do your scratch project, upload it and then be done. But if you get some time or just want to procrastinate some night by all means flip through the 300 plus scratch projects that your classmates made. Because there's some really nice projects that got uploaded there. And then finally, let's see if this actually came up. Yes. So what is -- has just popped up on the screen with a slight delay there is Elluminate the virtual terminal room. And this is again this glorified chat room. So at the top left it looks like teaching fellow Andrew Sellergren has perfectly timed this demonstration to say hello to us. So he is chatting with us down there. If I dare to indulge something like this. Hello Andrew, you are in Sanders right now to see if he's actually paying attention. At the top left you see who's logged in, both me and Andrew. Bottom left is obviously the chat window. The right hand side is where we can share screens but the workflow you will find is that if you log into this thing, you've got a question, you click this little icon here at the top left. The little hand icon, click that and then notice [Sound effects] a little 1 goes up next to my name. Which means David is first in line, which hopefully means that Andrew the teaching fellow will pull me into a private -- dare I turn around [Laughter] he'll pull me into a private chat room where I can then share my screen just with that TF or CA and so forth. So it's really neat, works reasonably well. Doesn't work so well over dial-up and we're done. [Sound effects] But realize it's a nice alternative to trucking to the science center when it's say snowing in November so just to ask that little quick question. So, why are you all here? So, we had 365 plus folks register for CS50, which is fantastic. We of course asked folks, why did you decide to take this CS50 in that Pset 0 survey? I thought I would give you a couple of choice excerpts. We love the serious ones but frankly the funny ones are more fun to indulge in lecture. So the first one was an answer to question 13. Why did you decide to take CS50? Quote, unquote. "I feel like an idiot when I use computers." [Laughter] So fair enough. This one I kind of had to read twice because I wasn't sure really why one would answer in this way. Question 13, Why did you decide to take CS50? Answer, I've never taken CS50. [Laughter] Thank you. Some of these can only be shown, not read also in answer to the same. [Laughter] So it seems we're not the ones doing all of the damage because clearly some of the folks are coming into the course this way. And then finally this one was fun if only for the all capitals. There you go. [Laughter] So thank you for those choice responses. On a more serious note for just a moment. So this course has a lot of fun. It does have a lot of work. And it is nonetheless an independent sort of work but very much a shared experience, very much an opportunity in office hours and sections and just in your dorm rooms to talk to other students about the problems that -- what you're struggling with, material related to it. But realize there is a line between talking about the problem set, reasoning through how you're going to approach it and the act of saying, "Hey why don't you just look at what I've done or hey here's my code." Or in an e-mail hey here is an attachment. So realize the course's syllabus and repeat it on every problem set second page is the policy on academic honesty is very clear over the years as to what's appropriate, what is not. Frankly the rule of thumb is talk all you want in English but as soon as the conversation devolves into specific C code, you probably are starting to come close to or crossing that line. Let me make the message ever so clear with this-- [ Sound effects ] >> So in the past two years this is the number of students this course had aborted. It gives us no pleasure to do so. But do you realize when problem sets are submitted we cross compare every problem set against every other. We have archives of years worth of problem sets, we cross compare all of that stuff. It's not hard, we have computers with which to help us. So please let me make one personal plea at the start of this semester. Please do not put your time at this wonderful place at risk by doing something stupid. We are very good at what we do and we take again no pleasure in doing so but this does happen and we will do it. So please meet us halfway. We're happy to have a lot of fun. We're happy to do a lot of work but do realize those constraints. So, let me try to take the tension off and do some fun facts about CS50 now. So we looked at Pset just because we like playing with data. We did this already with the Google map. What are you guys carrying [Sound effects] Pre is not doing so well it seems. This was the Palm phone that recently came out but a lot of you have normal phones or smart phones. A lot of Blackberry's and iPhones. Who are you guys using if only because it's an interesting tidbit. Nobody's using Nextel but that's really my fault because I forgot they merged with Sprint so that kind of makes sense. [Laughter] Verizon though is quite popular, as an aside I'll point out -- and some Googling should answer this for you. So Harvard actually has some pretty good discounts on a lot of these carriers, so if you do pay your own cell phone bill -- don't e-mail cs50.net for this but Google around and you'll see that you can get like 10, 19 percent off some of these guys. Completely irrelevant to CS50. Who are you? So historically you're mostly sophomores but we do have a pretty good range of folks here. The sample size for all of these charts is roughly 350, 400 people because we are also offering this course via the extension school as computer science E52. Hello to those of you tuning in from afar and it's to the extension school that we owe the very nice videos that are produced each week as well as one of our own producers. Oh, what else we got? So, dorms, mess hall. So I couldn't remember are students still living in mess hall? Or did they boot them all? Yes? But not here. Okay, so there's a few of them just not here today. So Apley is understandably-- Grays is a pretty damn big dorm and no one from Grays is apparently taking 50. But the houses, we have this demographic here and this is complete coincidence. So the most popular house in terms of taking CS50 this year is no longer Levritt [Assumed spelling] it is in fact Mather, which is perhaps --[Applause] which is perhaps not surprising because on this same survey when asked what house would you want to live in if given the choice everyone said, "Mather" actually. [Applause] So what else, random little facts. What are you using? Well there's diversity even in operating systems. A bunch of you have clearly upgraded to Snow Leopard, some of you have not. The most common answer in other is MAC OS10.4, which is fine but I think you can even get upgrades from Harvard as well from the helpdesk. Why are you taking CS50? So certainly allots for concentration credit. But what's really neat to see is just how many folks honestly are taking it as a pure elective and then also these days for GenEd or formerly core credits. Who are you? So this one is actually more useful I think to know, to reassure perhaps at this point in the semester. So this is consistent with last year's number. There's absolutely some people in here, maybe one of the people or you know maybe a tenth of the person sitting next to you who knows more than you do, if not significantly so. And that's fine. That's why we have the hacker editions of Psets. That's why we push the envelope sometimes in lecture with some slightly more advanced material but realize that the meat, the sweet spot here is those in between and those less comfortable. So if you still have this mentality that this person and this person both know more than you in this room, probably not the case just based on pure statistics alone. Prior courses, just too really hammer this through. There's this misconception on campus for years that almost everyone who takes CS50 has prior course work in computer science, AP computer science. Not so. What are you all whispering about? This is all of your data you know, so zero courses is the most popular answer here. So, welcome to CS50. That is who you are and we hope you enjoy your stay with us. So how do you write a program in languages other than C just so that you're clear that this is not a course on C. This is not going to be a course on PHP or JavaScript. It's an introductory computer science course that just so happens to use those several languages to explore what are ultimately some really interesting and really fundamental topics. Well you don't have to necessarily use C to go about implementing programs. The canonical program for instance in Java is something that might look like this. If I want to write hello dot Java I can use -- actually I'll use the program familiar. Hello dot Java. So Java has different conventions, usually capitalize the file name. It ends in dot Java and frankly this is where languages like Java get a little annoying though Cs not much better. First we need to say class hello, whatever that means. Then we say public, static, void, main, string, bracket, args. But there's some shared features there right. There's this main keyword. There's the square brackets. Arg sound like argv so there's definitely some similarities. And now if I want to go ahead and say print something it can be printf because there is a printf function in C but it's usually system dot, out dot, print line and then hello world, exclamation point, semi colon. It's pretty similar. So I'm going to go ahead and hot control X to save this, Y to save yes. And now I'm going to run Java C, which is the Java compiler on hello dot Java. It seems to take a moment then I have in my directory here not only today's other source code, for which you have printouts but notice from this file came a file called hello dot class. Because in the world of Java you say Java and then the name of the file without that file extension enter viola. So my first program in Java. So just realize when we introduce certain features here if they're not necessarily specific to C and frankly the only -- as I may have said 2 weeks ago, the only language -- actually I said this outside of class to someone. The only languages I think I was ever taught quote, unquote was C in CS50. A little bit of C plus plus and Lisp in CS51 and then pretty much after that everything I've learned since has been on my own. PHP, JavaScript, I never took courses in these languages. PHP I literally learned by IMing a friend back and forth over the course of a weekend asking him how do I do this, how do I do this and started to finally RTFM the manual myself when he advised me to do so. [Laughter] So realize that what you'll get is this ability to parlay a lot of what you learn in this course to other context all together. Lisp is another language scheme, something similar. You'll be introduced to those in different courses but Pearl, PHP these are languages so similar to C that you'll probably pick them up fairly easily. So bugs, it turns out and actually this is a bit of a myth or white lie but it turns out the first bug in a computer program was in fact a bug. There was a computer at Harvard called the Mark 2. Grace Hopper was the engineer who was among the people responsible for this machine and as the story goes there was a problem with the Mark 1 computer at one point. And sure enough when some humans went inside what was literally inside of the computer, it was in fact that large they found a moth that was wrecking some havoc inside. And now supposedly bug had been used for years prior to describe problems with machines and so forth. But to debug is supposedly perhaps one of the treasures we're left with from that particular story because the act of debugging this computer was literally the act of debugging this computer. But with that said let's actually look at what's a more modern type of bug in this program here. So you have this printout, this is buggy 1 dot C. Let me ask that you turn to the person next to you if you don't -- turn to a person next to you whom you do not know, say hello and then together figure out what the buggy is in this program. Take about a minute. [ Sound effects ] >> Okay, I've been waiting to do that for like a minute. Alright, so what is the bug? Anyone who found it. [Inaudible background discussion] So it prints 11 stars instead of the 10. And it's simply because programmers tend to count from 0, which this person has done but they're counting up to an equal to 10 and if you actually do the math 0 through 10 inclusive is in fact 11 stars. So there's the bug there. Excellent, that was some pretty good chatter. If you weren't among those who kind of realized what trajectory we're on this second one is maybe a little easier. And let's just see if you can spot it. Don't shout it out just yet. Let everyone flip. And now what's the bug? Yes? >> [Inaudible background discussion] >> Sorry. Can you say it a little louder, just a little loud back here. >> Prints 11 vertically. >> So it prints 11 vertically but it doesn't, it supposed to. Why -- what's the problem here? >> [Inaudible background discussion] >> Yeah so I've forgotten the curly braces. So if you notice the program kind of looks okay right, here's a loop and yes it does prints -- okay 11 again so we can fix that. So same bug, we didn't actually fix that problem here. But a quick fix there gets rid of that but if this is supposed to print 10 stars, 1 per line -- let me go ahead and check. So GCC dash o buggy 2 and then buggy 2 dot C is again my compiler statement. Now I can run buggy 2 enter, they're all on the same line. Not one per line and yeah so remember even though you might indent because it looks pretty, you've been told it's a matter of good style, the computer doesn't care. So again this was that subtle thing that now encasing this in curly braces means don't just execute the next line, execute the next bunch of lines and this should if I rerun it fix that. [Sound effects] Okay, right. So again here to common goof early on, I need to recompile. Same exact command, now I can run buggy 2 and I do in fact get 1 per line. Excellent. So that's a little warm up exercise, they'll get a little more sophisticated. Let's introduce a new capability. So we knew from the very first week of class that we have this ability to represent data in the computer, numbers. And we do it with this, turn on the switch. Turn off the switch. You have some means of storing bits inside of a computer. From where does that come, that capability? Well computers are generally plugged into the wall or they have batteries, so there's some aspect of electricity underlying any computer. For today's purposes you can assume that with electricity can you represent two states very easily. On or off. There's either some electricity there flowing or there is not. Or technically there's some electricity that's tripped a transistor, a little switch, a tiny, tiny little switch that turns a state on. Or no such electricity flow to the transistor stayed off. So very simple ultimately to represent two different states in computers, not that useful if all you can express though are 0s and 1s. But thankfully we had our bytes of volunteers come up and by raising or lowering their hands those 8 bits represented a byte. But that byte, a number could be mapped to a letter by way of that little coding scheme called ASCII, A-S-C-I-I. Just a standard the world came up with years ago that just says capital A is 65. Random trivia but actually useful because from there can you bootstrap yourself to most of the other capital letters and 97 is lower case A. So there's clearly a way of representing letters with numbers but how do you the programmer get access to that kind of capability? Well through this ability called typecasting and we talked briefly last week about typecasting. But we casted things like ints to floats so that we could actually use a decimal point. But if you know, you the computer scientists know that this character, the capital A, I know this is just the number 65. How do I get at that number? Well you can tell the compiler, "Give me the numeric value of this character" by way of typecasting. To typecast one type to another means to change it if it's possible conceptually from one type to another. So here's two lines of code clearly out of context, clearly not a real program because we have no main function or anything like that. But here's two lines of code, int I is going to get the integral value of quote-unquote A and by contrast or conversely C, the little c variable is going to get the character value of 65. So this is just showing you one direction and then the other direction but notice the single quotes. We reserve double quotes last week for strings and a string is anything that has, for simplicity let's say two or more characters. But you can have strings that just have 1 character but more on that later this week. But single characters like this, like A are just single quotes. So let's actually make use of this information. Here's a little demo, ASCII 1 that's going to show me the mapping. So that's actually -- I'm kind of overwhelmed by the code there so let me go ahead and go back and do GCC dash O ASCII 1 and then S. That's a little gap there, sorry. [Laughter] ASCII 1 and now let me go ahead and run ASCII 1 enter. Okay, interesting a little overwhelming but okay, looks like this is just a stupid little program that prints a chart of alphabetical letters and then their corresponding ASCII integral values. So how would I do this? Well hopefully I didn't copy and paste the same line of code 26 or 52 times right. Hopefully there's a more intelligent way and there is, just from last week when we introduced loops. So we have this ability here, we're declaring I just because I wanted a variable up front that I wanted to use twice. So I said, "Let's put it up front." I didn't bother commenting it because to put a comment for int I, you know you could comment it but there's a line there somewhere where it's a little excessive maybe to comment something like I. But let's see. What am I doing here? I've got comments telling me. So let's see, four I gets 65, I is less than 65 plus 26 and then I plus, plus. But remember the order of operations there, so with the four loop the initialization happens, the condition gets checked then the stuff happens. Then the increment happens and then you repeat except for the initialization, which as the word implies only happens once, when you initialize. So it looks like this loop starts I at 65, it prints its character and integral value how? Well let's see. Printf here is saying print a character with this little format code, then print a colon literally, then a space literally, then a decimal integer and then a new line. Well what do I want to swap into each of those places? Well not just I but the character equivalent of I. So I typecast I but then at the end here I just want I itself so I just string it in. Alright, so pretty straight forward and then in fact the stuff down below is identical except I started from 97 and went on up. Now there's no need, realize to hard code things like 65 and 97. This particular example is just meant to demonstrate the mapping because we can actually generalize this. But all this is doing is showing me that in fact what we preached 2 weeks ago is clearly the case underneath the hood. Well let's look at a slightly different example. So this is the second version, ASCII 2 and I got a little fancy because what I didn't like a moment ago was when I ran ASCII 1, it's just unmanageable. The big font size, small font size it just doesn't fit. Let me see if I can start using my formatting strings to lay things out a little more cleverly. So let's see this time I just have 1 loop from 65 up to 26 places later. And then okay I got a little creative here, so I have character -- a character, some space, an int, a bunch more space and then 3 D so there's no decimal point there, so what might it mean in a format code to say 3 and then int, the little D? It's not specifying number of decimal places but -- Yeah? >> [Inaudible background discussion] >> Yeah. Exactly. So the numbers that we're going to be playing with might actually over -- if we're starting at 97 for lower case 97, 98, 99, 100 which is 3 digits whereas 97 is just 2. So if we're iterating over this loop but I want to exercise some control -- problem with playing music during class, just hung up on FASIT. [Laughter] That was my phone if you didn't realize what was ringing. Apparently I'm the only one anyway. No comment about the phone in the future. [Laughter] So what I want to do is keeps this formatting pretty, so let me see. So this going to say put an int here but reserve 3 places for it not just the standard 1 or 2 or 3. So what am I plugging in after that F character? So I've got 4 format codes, the first one's going to take the character equivalent of I, the second just I. Oh this is interesting, I kind of did some math and realized you know what? The lowercase letters are always the same distance from the uppercase 97 is 32 places away from 65 if you just do the arithmetic. So you know what? I don't need a separate loop to do the lowercase letters, I can simply do both at once just by using a little clever arithmetic. An offset if you will because the first time I print I's character and then I but the second time I print I plus 32 and then its character equivalent so it feels like what I'm going to get here is a slightly more interesting chart. So let me go ahead and run ASCII 2, I'm going to compile its code here, thought C, okay, compiles okay, ASCII 2, enter, okay. So baby step toward something a little more interesting but definitely more useful, cleaner. And notice now what I was getting at with the format code. 97 just takes 2 places, then there's 98, 99, everything kind of lined up nicely. What if I had not done so, let me go back in here and sort of not realize this capability. Change that to what I know from last week, recompile, recompile as -- rerun ASCII 2 and [Sound effects] it kind of gets messy. It gets ugly quickly and if you think now about how you're going to present any textual information for problem set 1, you can think of your terminal window. This SSH window for historical reasons is kind of a terminal window and by default it's typically 80 characters wide and 24 characters tall. Now on 30 inch monitors, 20 inch monitors whatever these days, you can clearly drag a putty window or drag a MAC OS terminal window to be much, much larger than that. So this is somewhat historical but for the first problem set we specifically for the Mario pyramid will tell you to assume only a certain screen size so that you can actually present the information in a predictable way. So I would argue that this is just ugly, you know it's not wrong but we can do better. And that's again something so simple. The format code instead of doing point and then a number then d -- rather point, a number then F. We just do a number and D and that just gives you the width of the place that you want to reserve from some -- for some variable's value. Alright let's take a look at a third version here, so this one is slightly more clever. I don't need it to do so much math in the second line and I can kind of integrate it all above. You know what? If characters are just numbers, well why am I doing this back and forth and back and forth? Why don't I just iterate over the characters themselves? Now arguably the first approach was pretty darn clear, so realize this too is meant more for instructional purposes than for here's how you should have done this. But let's see what we can do because C does give us this low level access to characters representation. So a 4 loop is initializing a character C to quote, unquote A. Turns out because C is just a number you can say, "Is this character less than or equal to Z", quote, unquote, single quotes. And then just to be ever so clear I did show the typecasting here. On each iteration you don't have to do something like I plus, plus. What you can really do is a whole expression and I will push the limits of my handwriting here. This I plus, plus is the same as doing I equals I, plus 1 so I could put either of this or this in the update field [Inaudible]. It's just the world and myself prefers the top left one just because it's a lot sexier, it's a lot simpler, it's more compact, I plus, plus. But realize I'm taking advantage of the latter approach down here. I'm saying C gets what? Well what do I want to do? I want to first cast C to an int to get it to a number. Then I want to add 1 to it, okay but I don't want a number now I have to cast the whole thing back to a char because I'm assigning it again to the variable C. Now it turns out that I actually don't need all this typecasting. This example has it just for clarity as to what the casting -- what casting is going on. But a lot of this typecasting can and will happen implicitly even if you leave it off. But again the takeaways for now are just that there is this equivalence. But let's do something that makes this slightly more compelling, so Battleship. So at first glance we clearly have some more code here, looks a little cryptic but again I see nothing on the board that we haven't seen in other context already. Some printf's, some for loops, some variables and really that's it. So a common, you know motivation in a course is -- you know I don't want to just write hello world, I don't want to just convert ASCII to integers and vice versa. Let me make a game or just like you did with Scratch, many of you wrote games not because it was the only thing you could do but just because it's more interesting than just doing you know, a cat bouncing back and forth. Well if we needed to implement a game in this context of C like Battleship. Well one of the things we might do is actually represent a board. So let me go ahead and compile Battleship and I'm going to start doing it the quick approach. So Battleship, I'm just going to type make and I'm going to let the computer figure out what the command is so now I can run Battleship. And let me make one note about security actually, since some of you might have realized this already. A typical computer scientist or at least Linux geek or MAC OS geek will write commands. If they've written a program that they know is currently stored in their current directory they will be ever so explicit and say, "Run the program that's in my current directory, which is symbolized as dot, slash and then Foo if Foo is the name of the program. And this is actually a security thing so that when you connect to a system with SSH, if you just write something like Foo thinking, "Oh Foo is a program in, my directory." Well what if Foo is actually a malicious command that some bad guy put on the system? They just tricked you into executing that version of Foo instead of your own. But for now in the course you can just write your program's name at the prompt whether it's ISBN or Mario just because that sort of adds an unnecessary complication. But when I'm doing it, it's just a matter of habit and good practice. So there's the game of Battleship. So this was that little childhood game where each -- you and your friend have this little laptop type thing with a bunch of plastic holes and you say, "A2" and then they put a puzzle piece in and then go [Sound effects] when there actually a ship in the hole. Well if I want to represent this here I've got A through J on the left, 1 through 10 at the top and then I went with the ASCII Art as it's called. So if you really want to geek out Google something like ASCII Art and the world has wasted so many hours over the decades writing pictures, writing -- depicting people in photographs just using the characters on the keyboard. So this is a little bit of ASCII Art but there's clearly some complexity there at least for out purposes today, because I've gotten letters being incremented on the left, that's kind of interesting but I also have numbers incrementing on the right. Everything is formatted nicely so let's just tease this apart slightly. This is Battleship dot oops, what did I do wrong? >> [Inaudible background discussion] >> Right. So I tried changing accidentally the binary, the 0s and 1s version not the source code. So here's the source code. Let me scroll down and just think how would you approach this? Well if the goal for a problem set, if this is how you're beginning is to implement something like this, that you have a picture in front of you or you have the game in front of you, you want to implement this. Just think through how you would implement this. Well this is a screen that clearly for the past week and a half has only let me send output to the screen from left to right and then from top to bottom. Thus far I have not been able to jump back and fix things. Once I print a new line, that's it I'm on the new line just like a typewriter. So what could I do first? Well what's the first thing I should probably print then in my program, if I want to replicate this? So probably the numbers on top, right. And that's probably not that hard. Even if you haven't even dived into problem set 1 yet you probably know that if you copied and pasted some of the code from past examples, the main line and then the curly braces, you could probably write the 4 loop that iterates from I equals 1 up to and equal to 10 and just print that thing on the top row. What about the next row though? Well if we ignore the letters it's really damn easy to print the grid of holes in this board, right. You just iterate from left to right and print 10 lower cases -- O's followed by a little white space after them. But the interesting part seems to be the letters on the side. So let's focus then on that. The top part here as the comment implies, print top row of numbers, there's really no magic there. It's boring at this point. But now this part's a little more interesting because it turns out you can nest in loops in C. Just like some of you might have nested forever loops or loop puzzle pieces in Scratch. So here we have it. Let's see for I, I want 10 holes anyway so I'm going to go from 0 up to 10. I could go 1 through 10, really just as two different approaches to the same problem. Now what do I want to do? Well the first thing I want to do on every one of these 10 iterations because I want 10 rows of little O's, I want to print out the letter. So lets see, character and then some space. What do I want to print? Well I want to start with A but you know what, I want to just do an offset. I don't need to hard code in A, B, C, D. Let me just start with A and then add to A the value of I, but if I do A plus I where I is 0 what do I get back? Just A right. So actually it makes sense that I started with I equals 0 here because I'm intentionally using it not as an increment yet but as an offset from the capital letter A. So this line of code here, if I just do this. Let me go and remove everything except that line and the new line below it. Let me go ahead and recompile and now notice the compilers saying, "Whoa you didn't use J." That's okay because I just removed the code that might have used J. Let me re-run Battleship, okay. So that's a nice baby step and this is not meant to belittle the process of coding. This is precisely the approach that someone should take in this class especially those less comfortable. When you're trying to tackle a problem set don't try and implement the whole damn program at once, just get something working right. And we did, the top row was so easy. Second row took a little bit of you know, thought, might have taken 10 minutes, 20 minutes, half an hour to get it right but now we have almost everything. We just need to do a little more on that second loop because we need to fill the rest of the row with O's. So how did I do that? Well let me put my code back and if what I'm doing in the outer loop with the variable I, is going from row to row to row to row. Well if I need to simultaneously, before going to the next row quickly run from left to right spiting out some 0's well that just calls for another loop. So here it is, for J equals 1, J up through and equal to 10, J plus, plus you know what this was actually pretty easy. Just print out the lowercase O followed by 2 spaces because I decided aesthetically that looked nice. And in the end I put a new line, another new line at the very end to make it all very pretty. And if I re-run the compiler and then re-run Battleship, in fact I have this entire board here. Any questions about this? Well you can't help but get excited when the next topic somehow involves Homer Simpson. But let's go ahead and take our 5 minute break here. We'll resume in 5. [Sound effects] So one quick announcement. The Harvard Computer Society, two of whose members you met last week are going to be offering one of several seminars tomorrow, Tuesday at 7:00 p.m. in Maxwell dorm in the computer science building. The specifics are on the course website's homepage right now. CS50, net but this is entitled demystifying Linux. And this is meant as just a free optional seminar. If you would like to join those folks tomorrow and just learn a little bit more about Linux. This course does not assume that you know Linux. We will provide everything you need to know in the form of sections and the problem sets PDF's themselves. But if you like this kind of stuff, you want to learn from some particularly geeky folks how you can do yet more at the Linux command line do feel free to check that out. But for now our focus will be on Homer here. So this is perhaps one of the most annoying songs ever. There's this classic Simpson's episode, which I -- if ever there was a Youtube moment it would be this one. Where Otto is actually driving the bus listening to 99 bottles of beer on the wall and then all of the sudden you realize it's only in his headphones for some reason. But the motivation here is that annoying though this particular song is, there's a pattern to it. And it suggests that there's an opportunity to just deploy some of these basic building blocks we've been looking at like loops and variables and so forth. And so let me go ahead and open up one possible approach to implementing this song. Right. The most naive approach would be to write a text file that contains the entire song four lines at a time. Copy, paste, copy, paste, copy paste and just change the number manually each time. But finally now do we have some programming chops with which to do this. So the programs are going to start getting a little longer but not because they're fundamentally more complicated, they just need to do a little bit more. So here's my main routine, I want to print out the 99 bottles of beer on the wall song. Well let me go ahead and first ask just because hard coding 99 is even less interesting, let me at least ask the user for some input. How many bottles of beer will there be on the wall? So I'm going to use the CS50 library. I'm calling get int, I'm putting the return value into N. And how do I use the CS50 library? Well again besides using standard IO, which you need for print F we use CS50 dot H for functions like these. Alright so the start of this program's pretty straight forward just ask the user for an int, I could have put any sentence here I just chose to state it in the form of a question. And notice the silly little aesthetics, I put a space character at the end just so that the blinking prompt would stay at the same line and not arbitrarily go to the next one. But these are just aesthetics. Well here is an example of error checking. Thus far we've just assumed that the user's going to be cooperative but in general that's a very bad thing. And most always when you are using your own MAC or PC and something goes wrong, a program crashes, it hangs or just something weird happens that's because a programmer somewhere in an office somewhere far away made some mistake. Made maybe some stupid mistake because he or she did not anticipate that you might hit that key on the keyboard at that moment and time. Or even though you were told to enter a number, like a positive number, you know you accidentally make a typo and some punctuation goes in or you are a malicious user and you're intentionally typing in a word instead of a number when prompted. Well you the programmer now for this course, for the problem sets and just in general absolutely have to code defensively. And you have to assume that the user is either an idiot or just outright malicious because only then can you genuinely write code that would stand just ordinary use, right. Even in this course, right, 300 plus students we had on the little survey for problem set 0. Put your FAS user name here, omit the at fast at harvard.edu right. If their sample size is long enough you're going to have someone like doofus at FAS at harvard.edu input it into that field. It happens right. So fortunately we have programming seals, we can strip out that kind of text. But again this speaks to just the need to code defensively your own, you will be happier for it because it's now we the humans who have to go and clean up that data. Much better if we had been able to defend against that with -- by rejecting that kind of input at the get go. But because we outsourced to SurveyMonkey we didn't have that ability. So here's a little sanity check. A little bit of error checking. If N -- if you're that person doofus at FAS please stay on the course, we're not trying to pick on you. [Laughter] So if N is less than 1, it's so easy to check some of these inputs right. Get int last week, we already saw will reject non-numeric input. Non-integral input because we the CS50 staff made sure that it prompts you to retry, retry, retry if the user as we saw tries typing in monkey instead of a number. So I don't have to check against -- for word input or punctuation but numeric is one thing. If I actually care about the sign of that value, positive, negative, 0 I gotta do that myself. So if N is less than 1 I'm going to yell at the user, sorry that makes no sense and then I'm actually going to return 1. So this is a sneak preview of the capability I promised existed -- oops. There is the ability in C for functions like get int to return values. Similarly can main the default function that every program has, that's the function that just gets run by default. So does main in fact have what's called a return value and even though we've been sort of taking it on blind faith thus far, I've always been writing the word int before main, which means this main function that again I've been copying and pasting thus far does in fact return a value. It's just I'm not doing anything with it. But now think back to the real world. If you've ever been using a MAC or a PC and something goes wrong, often, not always but often you get a little prompt saying an error occurred or system error occurred or whatever. And then there's usually a number like a numeric identifier, maybe it's negative, maybe it's positive but it's usually useless to you the human unless you actually get on the phone or on e-mail and report that error to the company. And even then they probably don't care from an individual user. So that was probably a return value or an error code of some sort and even you and C, even with the simplest programs have this ability to return error codes such as this one. So because this is the first error that my program might encounter I arbitrarily but conventionally decided that the first error is going to return 1. Now I won't see that, 1 will not get printed to the screen. This isn't printf. This is return so only a particularly sophisticated person or programmer could actually see that number. But you a couple of weeks from now will be using a special tool called a debugger. GDB, Visual Studio you might have used in other classes but this is a program that can see this information and you'll find that it's quite useful. So for now it's just a convention that I'm adopting on blind faith, more to come. So now we've done the setup, half of my program really has just been devoted just to the setup portion here. Here's the juicy part of the program, sing the annoying song. Well first aesthetically I decided to print the new line right here, not that interesting. Now what am I doing? Well here's the for loop in which I'm counting down just because that conceptually was easier for me than counting up for a song like this. I'm initializing I to N, I'm going to keep singing as long as I is greater than 0 and then just as I can increment so can I decrement as we've seen before. Now what do I want to do on each of these iterations? I want to sing those stanzas, so what do I want to do? Printf. So how many bottles and now I'm cheating on the grammar right because once I get to like 1 I don't want my grammar to be wrong. So I parenthesis the S as sort of a corner cutting here of beer on the wall backslash N and then I plug in the value. So that's old school now. That's pretty easy. Fortunately the next line of code is pretty much the same thing verbatim so it's a copy, paste with a change in the wording. Take one down pass it around, that's just a string that always get printed so it only gets a little interesting at the last line. But again I know arithmetic, I know I is a variable so print out I minus 1 because the last line of the song per the little canonical form here is to say 98 bottles of beer on the wall. And now I repeat, add nauseam until at least until I bottom out at 0. So if I go ahead and close this, let me make beer 1, let me go ahead and run beer 1. How many bottles, let's say 99, enter. It's pretty damn fast right. That's kind of the neat thing when you have 2 gigahertz, 3 gigahertz computers that allot really quickly at least in this case. But it seems to have worked correctly but that's not the only way we could implement this. Let's take a look at a slight variance. So this is version 2. Let's see this looks the same as before -- oh interesting. So this time the -- I the programmer decided [Sound effects] I like while loops. I can do the same thing in a while loop, let me prove as much. So here I said, "Sing the song while N is greater than 0." And maybe that's you kind of speaks true or it just makes more sense. Do the following while there are positive numbers of beer on the wall. So a while loop is perfectly fine, the only thing you have to remember to do is 1, make sure that you manually decrement N at the end there. And now I am doing something slightly different. In version 1 whose value was I changing on every iteration? So I, I was doing minus, minus. Here I'm kind of being quote, unquote destructive. I'm taking the users input proffered at the very top of the program by a get int, storing it at N but at the end of this program I have lost track of what? Like what that original value was. Now maybe that's a problem if especially at the end of the song you want to say something like thanks for playing with 99 bottles of beer. But in this case where I don't ever use that value again, the short answer is who cares. I had a variable, I chose to decrement it destructively thereby losing track of its original value but again who cares, it doesn't really feel wrong because I never need that value again. But if I did then there would be a problem and I might need to introduce another variable. And one quick word on style, so as much as I preached already using well named variables like we did with previous programs. It is completely reasonable and conventional to use basic place holders like N whenever you have a total number of things. People like N, I is almost always used as an increment. If you've used I already as we did for Battleship because it's nested I went with J, then you go with K, then L, then if you're nesting that deep you're probably doing something foolishly. At which point, yes you could go to M but things just start to feel a little weird there. But actually let me roll back to that just to be clear. This was Battleship, recall. Notice bad things would have happened if I had used or reused I here, no. Like if I had been taught in class, you know use I. So I say for I gets that. I is that. I is that. Okay that seems fine so let me go ahead and recompile Battleship. Okay unused variable J but that was to be expected. Battleship and [Sound effects] broken. Right. So just to realize-- just realize that because I was declared already if you're incrementing it in this outer loop in Battleship you better make sure you know what you're doing if you're also going to increment that in a nested loop because clearly bead things or unintended things happen. And that's why I just whipped out J instead of I for that particular program. But in beer 2 the only fundamental difference here was that I used a while loop. Why? [Sound effects] It's different. It's another way. Well there's yet more ways. Let's take a look at beer 3. Alright, so beer 3 okay, on intro -- oh interesting. Little teaser here , I seem to be about to use strings. Let me see in what context. Fast forward, oh I got a little anal in this version. So I wanted to use proper grammar, which is not unreasonable in fact if you have the ability now to fix what people generally punt on in software just because it's easy to cut corners. Why not do it? And in fact I introduced this here just because it introduces a neat piece of syntax that you don't have to use. But the more comfortable you get with this stuff the sexier lines of code like this tend to feel, at least to some of us. So it looks like S1 and S2 were strings because we saw that up above, they were both declared up here as stings. And remember a string, trivia, is equivalent to what in reality? Charstar right. And eventually we'll take that training wheel off but the CS50 library kind of hides that now and calls it a string. So what does S1 equal? S1 equals, hmm. I equal, equals 1 question mark, bottle, bottles, this is a little weird and then where am I using this. Okay S1 is here. S1 is here. S2 is there, alright. It's a little confusing at first so let me go ahead and save and make beer 3. Compiles, okay. Let me go ahead and run beer 3 and lets do it with a few numbers so we can actually see everything on the screen at once. Oh, okay. So again bottles of -- 3 bottles of beer, 2 bottles. Oh here's where it actually seems to have made a difference, 1 bottle of beer but 0 bottles. So apparently I need to have both S1 and S2 because even within the same chorus I need to spit out a slightly different version of the word potentially. So what's happening? Well if in this sentence here, the very first where I spit out S1, something bottles of beer on the wall. Under what cases do I want to say bottle versus bottles? When do I want to say bottle? So only when the number of bottles of beer on the wall is 1 right. That's what's grammatically correct. 1 bottle of beer on the wall. So what we can infer now, what this new piece of syntax is doing. So this is technically called a ternary operator. You've seen binary operators before even though we didn't slap that label on them so X plus Y [Sound effects] plus is apparently a binary operator. It takes something on the left and something on the right. This is a fancy thing called the ternary operator because it takes something on the left, something in the middle and something on the right. But for now that's just jargon. But apparently what this is doing is kind of like a condition, it's doing it all on one line. S1 is being assigned the variable -- the string bottle if I equals, equals 1. Otherwise, else it's being assigned bottles. So it turns out this slick piece of syntax is actually equivalent to this. If I equals, equals 1, S1 gets bottle else S1 gets bottles. So this is now equivalent to this first line. And then the second line is the same exact code but with S2 instead of S1. Why did I do it? Well again it's just a matter of style. This -- these four lines [Sound effects] they just take up four lines for no good reason. I can kind of collapse this into something much more elegant, not strictly necessary but that's all this is. So if you ever see this operator in any context, book or staff code it's just the same thing as an if and an else. And technically yes, if your mind is already racing you can nest these things so you can effectively implement if, else, if, else, if, else, like don't cross that line then it becomes unreadable. Do it for one, an if and an else and leave it at that. So what's the end result? Well again, how many bottles? 99. It's the same exact song but I fixed the grammar. Now just to think outside the box for a moment, why might you not want to do this? What's a downside of adding this check, in version 3 as opposed to just doing bottle parenthesis, S, close parenthesis as I did in the first two versions? Why might you not want to do this? [Sound effects] Sorry. >> [Inaudible background discussion] >> A little louder. >> [Inaudible background discussion] >> So if I started with 1 -- okay if I started with 1 I think it will still work. How many bottles of beer? So it still works but I've at least generalized the code enough that it will still work even in the case of input 1. Even though again it's not really doing much there because I've just chosen one number. [Inaudible background discussion] >> Yeah, so that's kind of the rub here. So even though we won't worry so much about this in this course because again even my laptop is 1.6 gigahertz, the servers we have are like 3 gigahertz. I mean even though we have really fast computers these days -- and even though these things are executed as we've seen in the blink of an eye. You know each of these two lines of code do require that the Intel CPU or whatever's inside your computer do some thinking and do some checks. And the sad thing is if you literally run this program a hundred times for 99 bottles of beer on down to 0 you're executing both of those if conditions again and again and again and they only change the behavior of the program once out of those 99 times. Now that's again not a fundamental problem but it's a tradeoff in terms of performance. So if later in the course you start working on things like cell phone applications for final projects. Or after this course you start writing code for little devices in cars or robots or little things where you don't have the luxury of many, many gigahertz of power, all of these little cycles, all of these additional statements start to add up. So realize that you know maybe this isn't the best approach for certain context but clearly here where we do have cycles to spend it seems a very reasonable tradeoff. Well lets take a look at a forth version of this here. Just to see how it might differ. Oh interesting. I've gotten rid of the chorus altogether. So this is this one new piece we'll introduce today. Anytime you sense that there's some redundancy in your code or there's this ability where you could outsource some piece of functionality just like we've been doing for printf right. You and we in class have not written a function that prints stuff to the screen. I don't frankly even know how I would start to print characters in a grid that's 80 by 24 characters wide. I just want to take for granted that someone else solved that problem so I can do something more interesting with it. Same thing for get int as you'll see in later weeks when we peel back the layer of the CS50 library and you see ho we implemented it. You'll see that [Sound effects] so many lines of code just to get an integer from the user, really takes the fun out of programming potentially. And that's why you'll see throughout the course and computer science programming that there's a reason that people have developed libraries and all of these header files. Because why did we invent the wheel, if you can leverage someone else's work like printf and get this and build you own far more interesting stuff on top of it. And that's related to this tidbit here right. It seems to be an opportunity for this stupid chorus that the kids might sing, let me just outsource that. Let me just let someone else generate all of the sentences that pertain to the chorus and the only thing I'm going to tell this black box, this little Oracle is the number of bottles of beer currently on the wall. And I want that entity, that black box to do the printing for me. So in fact that's exactly what I do and I simultaneously introduce just a couple of new tricks just for the sake of syntax. So it turns out in a Boolean expression you don't have to say while N is greater than 0 as I did in a previous version right. It's equivalent to just say while N because if N keeps getting decremented so N is 99, then 98, then, dot, dot, dot, then 1 then 0 at some point in the story you will have while 0. But 0 is false by nature so even though you don't have the less than or the greater than sign in there it's equivalent to just terminating the loop. So that's why I can get away with this cleverness here and then this cleverness is arguably confusing here but lets introduce the syntax. Chorus is a function so just as main is a function, which you kind of have to write because otherwise your program generally is not going to do anything. You can write your own functions just as someone else wrote printf and just as the staff wrote get int and get float. I or the person who wrote this code decided to call the function chorus, so apparently there's a function in this world now called chorus that if you pass it an integer it's going to presumably print the chorus. But you what I also just want to be clever here. I didn't really love this code you know, again a little anal I feel like this is [Sound effects] just why the curly braces, why the additional line? Now it's perfectly clear and it's you know, 100 percent credit. Were not preaching that this is bad code, bad design, bad style. Perfectly acceptable, but realize especially when it comes time to read other people's code whether it's in a book or in a class that this has the same effect. So this is where that big ugly table of precedence and operators comes in handy as a reference in the long run. You can determine using that or just trial and error, what's happening? So in fact what happens in this context is the value of N gets passed into chorus, the function just as a variable would get passed into printf. And then after that function is done doing its thing, then N simultaneously gets decremented. So it's the same as the code I just wrote and then deleted. So what in the world is chorus? Well here's how you write a method. Wow that code actually looks familiar. I apparently ripped out the code from main, put it later in the file down below and I simply wrote this. So I wrote chorus and then what did I do? Well chorus has to be parameterized, it needs to take input and inputs to functions are called parameters or arguments. So B, what is B? B just denotes bottle. I could give this parameter any name I want, so it's essentially a local variable. So anytime a function takes input you gotta tell the compiler what do want to call that input? So that you the programmer can actually use it. What is the type of the input? An int. So long story short, just as printf is a function that takes a format string on the left and then a comma separated list of variables on the right. So does chorus, apparently take one argument, just an int so that's past in. So once it's passe in I can refer to it as B. What do I do? Well this code is just ripped off from the previous example. I declare a couple of strings up here, then I assign them using my fancy little notation here. This code is the same as before, it's the same code. I've just factored it out. I've hierarchically decomposed, fancy way of saying it that you'll see in the textbook. Decomposed my code into now this separate function because you know what, it just feels a little cleaner. There was this big ugly block of code in my program, feels perfectly reasonable to factor that out because it really collectively, the seven lines of real code they accomplish a single goal that can be well summarized in a sentence. Sings about specified numbers of bottles. So anytime there's this opportunity to factor out a piece of code, a function is the direction that most programmers would go in. Now there's one last piece of detail here, void. What do you think it means if the word before the function name here is void? It just means it has no return value. Get int by nature returns a value right, that's a good thing because you use it to get a number from the user. But chorus does not need to get any input from the user. It just has this quote, unquote side effect of printing something to the screen. So for now you can kind of simplify the world into two buckets. Functions that have side effects, just print stuff to the screen that if you weren't watching that you would never know they did anything. And then functions that have return values. So return value here is void as in this case. Return value here is int, or string, or char, or whatever the case may be. We'll see we can do anything we want. So this is why up here I just call chorus just like I call printf. I don't have to assign its return value to a variable because it's not actually returning a variable at all. So let me go ahead and print out the results of these statements here. Let me go ahead and quit. Let me go ahead an make beer 4. Let me go ahead and run beer 4 here. 99 bottles of beer and viola the same exact example. So why don't we end on that note today and we'll see you again on Wednesday. [Applause] ==== Transcribed by Automatic Sync Technologies ====