[Script Info] Title: [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,0:00:01.38,0:00:03.65,Default,,0000,0000,0000,,Now let's talk about something \Nyou've been using Dialogue: 0,0:00:03.65,0:00:05.68,Default,,0000,0000,0000,,this whole time: Functions. Dialogue: 0,0:00:05.68,0:00:09.14,Default,,0000,0000,0000,,Whenever you've used commands \Nlike rect() or fill() or text(), Dialogue: 0,0:00:09.14,0:00:12.38,Default,,0000,0000,0000,,you've been calling functions, \Nand those functions have drawn Dialogue: 0,0:00:12.38,0:00:15.13,Default,,0000,0000,0000,,what you've told them to do. Dialogue: 0,0:00:15.13,0:00:16.81,Default,,0000,0000,0000,,What is a function really? Dialogue: 0,0:00:16.81,0:00:19.48,Default,,0000,0000,0000,,It's a collection of code\Nthat we've grouped together Dialogue: 0,0:00:19.48,0:00:21.24,Default,,0000,0000,0000,,and given a name\Nbecause we want to be able Dialogue: 0,0:00:21.24,0:00:23.17,Default,,0000,0000,0000,,to use that bit \Nof functionality many times. Dialogue: 0,0:00:23.37,0:00:25.98,Default,,0000,0000,0000,,Think about rect()? \NWhat does a rect() do? Dialogue: 0,0:00:25.98,0:00:28.55,Default,,0000,0000,0000,,It just draws four lines, right? Dialogue: 0,0:00:28.55,0:00:31.14,Default,,0000,0000,0000,,We could just do that \Nusing our line() function, right? Dialogue: 0,0:00:31.14,0:00:33.85,Default,,0000,0000,0000,,And here we have \Nwhat looks like a rectangle. Dialogue: 0,0:00:33.85,0:00:36.75,Default,,0000,0000,0000,,But we realize that we \Nkind of want to be able Dialogue: 0,0:00:36.75,0:00:39.32,Default,,0000,0000,0000,,to draw a rectangle a lot of times, Dialogue: 0,0:00:39.32,0:00:40.77,Default,,0000,0000,0000,,and it would be really annoying Dialogue: 0,0:00:40.77,0:00:42.78,Default,,0000,0000,0000,,to do the math every time\Nto try and figure out Dialogue: 0,0:00:42.78,0:00:44.28,Default,,0000,0000,0000,,how to draw a line\Nfrom one corner to the next Dialogue: 0,0:00:44.28,0:00:45.82,Default,,0000,0000,0000,,and next and next. Dialogue: 0,0:00:45.82,0:00:48.54,Default,,0000,0000,0000,,So instead, we just made \Na rect() function, Dialogue: 0,0:00:48.54,0:00:50.86,Default,,0000,0000,0000,,and that function does exactly \Nthe same thing Dialogue: 0,0:00:50.86,0:00:55.15,Default,,0000,0000,0000,,that those four lines of code did,\Nbut in much less code. Dialogue: 0,0:00:55.15,0:00:58.40,Default,,0000,0000,0000,,So that's pretty cool, \Nand rect() is one of those functions Dialogue: 0,0:00:58.40,0:01:00.52,Default,,0000,0000,0000,,that we've made available\Nfor ALL programs Dialogue: 0,0:01:00.52,0:01:02.73,Default,,0000,0000,0000,,to use here on Khan Academy. Dialogue: 0,0:01:02.73,0:01:05.34,Default,,0000,0000,0000,,But - you can also make up \Nyour own functions Dialogue: 0,0:01:05.34,0:01:08.41,Default,,0000,0000,0000,,to use in each of your programs. Dialogue: 0,0:01:08.41,0:01:11.43,Default,,0000,0000,0000,,For example - let's say \Nwe are making a program Dialogue: 0,0:01:11.43,0:01:14.43,Default,,0000,0000,0000,,and we want to draw Winston\Nmultiple times - Dialogue: 0,0:01:14.43,0:01:17.02,Default,,0000,0000,0000,,maybe cause we're going to tell\Nthe life story of Winston Dialogue: 0,0:01:17.02,0:01:20.19,Default,,0000,0000,0000,,and show him at every age\Nin his life. Dialogue: 0,0:01:20.19,0:01:24.24,Default,,0000,0000,0000,,So, here's what our Winston \Ndrawing code might start off as: Dialogue: 0,0:01:24.24,0:01:27.11,Default,,0000,0000,0000,,We have 'faceX' \Nand 'faceY' variables \N Dialogue: 0,0:01:27.11,0:01:29.03,Default,,0000,0000,0000,,to store the center of the face, Dialogue: 0,0:01:29.03,0:01:31.24,Default,,0000,0000,0000,,and then we draw the eyes\Nand the mouth Dialogue: 0,0:01:31.24,0:01:33.04,Default,,0000,0000,0000,,relative to those variables. Dialogue: 0,0:01:33.04,0:01:34.73,Default,,0000,0000,0000,,Right now the program \Nsees the code, Dialogue: 0,0:01:34.73,0:01:37.32,Default,,0000,0000,0000,,and it's not inside any function,\Nso it just runs it, Dialogue: 0,0:01:37.32,0:01:39.74,Default,,0000,0000,0000,,and it only runs it once. Dialogue: 0,0:01:39.74,0:01:43.72,Default,,0000,0000,0000,,OK, let's turn this into a function. Dialogue: 0,0:01:43.72,0:01:46.44,Default,,0000,0000,0000,,To do that, we do it \Nvery similarly to the way Dialogue: 0,0:01:46.44,0:01:48.49,Default,,0000,0000,0000,,we declare a variable,\Nbecause that's actually Dialogue: 0,0:01:48.49,0:01:50.24,Default,,0000,0000,0000,,what we are doing. Dialogue: 0,0:01:50.24,0:01:52.08,Default,,0000,0000,0000,,So we say 'var drawWinston'. Dialogue: 0,0:01:52.08,0:01:54.55,Default,,0000,0000,0000,,We give it a nice name,\Nvery descriptive, Dialogue: 0,0:01:54.55,0:01:59.04,Default,,0000,0000,0000,,and then '=', but here,\Ninstead of writing a number or a string, Dialogue: 0,0:01:59.04,0:02:02.72,Default,,0000,0000,0000,,we're going to write 'function'\N(make sure you spell it right) Dialogue: 0,0:02:02.72,0:02:08.36,Default,,0000,0000,0000,,and then empty parentheses '()' \Nand then an open curly '{' \N Dialogue: 0,0:02:08.36,0:02:11.56,Default,,0000,0000,0000,,and then a close curly '}' \Nand our semicolon ';' Dialogue: 0,0:02:11.56,0:02:14.49,Default,,0000,0000,0000,,OK so what we need to do\Nis put everything Dialogue: 0,0:02:14.49,0:02:19.45,Default,,0000,0000,0000,,that we want inside our function\Nin between the start and end curly. Dialogue: 0,0:02:19.45,0:02:22.13,Default,,0000,0000,0000,,So we're going to take \Nall this code here, Dialogue: 0,0:02:22.13,0:02:26.82,Default,,0000,0000,0000,,put it in our function\N(indent it nicely), and Ta Da! Dialogue: 0,0:02:26.82,0:02:28.97,Default,,0000,0000,0000,,So now what we have \Nis we have this variable Dialogue: 0,0:02:28.97,0:02:32.38,Default,,0000,0000,0000,,that is storing a function -\Nso basically we've given Dialogue: 0,0:02:32.38,0:02:35.76,Default,,0000,0000,0000,,a label to this block of code,\Nso that we'd be able to tell Dialogue: 0,0:02:35.76,0:02:37.86,Default,,0000,0000,0000,,our program at any time,\N Dialogue: 0,0:02:37.86,0:02:40.76,Default,,0000,0000,0000,,"Hey, find that block of code\Nwith that label and run it!" Dialogue: 0,0:02:40.76,0:02:43.55,Default,,0000,0000,0000,,We we're making\Nthis bit of code reusable. Dialogue: 0,0:02:43.55,0:02:46.54,Default,,0000,0000,0000,,But now notice, that we have \Nno Winston anymore! Dialogue: 0,0:02:46.54,0:02:49.36,Default,,0000,0000,0000,,We've lost Winston!\NWhere did he go? Dialogue: 0,0:02:49.36,0:02:53.02,Default,,0000,0000,0000,,OK - so what happened is \Nthat once we put this inside a function, Dialogue: 0,0:02:53.02,0:02:55.91,Default,,0000,0000,0000,,we told our program \N"hey here's a bunch of code Dialogue: 0,0:02:55.91,0:02:57.84,Default,,0000,0000,0000,,that I want to be able \Nto run later, Dialogue: 0,0:02:57.84,0:03:00.81,Default,,0000,0000,0000,,but only when I TELL you to run it." Dialogue: 0,0:03:00.81,0:03:04.29,Default,,0000,0000,0000,,So we have to tell it to run the code, \Nwhich means we need Dialogue: 0,0:03:04.29,0:03:09.11,Default,,0000,0000,0000,,to 'call' the function - just like we do\Nwith ellipse() and rect() and line(). Dialogue: 0,0:03:09.40,0:03:13.75,Default,,0000,0000,0000,,So we just write\Nthe function name {\i1}drawWinston{\i0} Dialogue: 0,0:03:13.75,0:03:16.38,Default,,0000,0000,0000,,followed by our start \Nand end parentheses '()' Dialogue: 0,0:03:16.38,0:03:18.96,Default,,0000,0000,0000,,and, of course, our semicolon, \Nand Ta Da! -- Dialogue: 0,0:03:18.96,0:03:20.94,Default,,0000,0000,0000,,We have a Winston! Dialogue: 0,0:03:20.94,0:03:24.24,Default,,0000,0000,0000,,OK! So I think it's cool, \Nbut you might not think it's cool Dialogue: 0,0:03:24.24,0:03:27.37,Default,,0000,0000,0000,,because all we've done is made \Nour program do exactly Dialogue: 0,0:03:27.37,0:03:30.24,Default,,0000,0000,0000,,what it did before. \NKinda silly huh? Dialogue: 0,0:03:30.24,0:03:32.92,Default,,0000,0000,0000,,The whole point of functions \Nis we can reuse them. Dialogue: 0,0:03:32.92,0:03:34.96,Default,,0000,0000,0000,,So let's do that now. Dialogue: 0,0:03:34.96,0:03:41.16,Default,,0000,0000,0000,,We can just copy and paste this function \Ncall... Ta da! Ta da! Over and over. Dialogue: 0,0:03:41.16,0:03:46.57,Default,,0000,0000,0000,,Hmmm, but it looks the same,\Nwell, it worked - Dialogue: 0,0:03:46.57,0:03:48.100,Default,,0000,0000,0000,,it's drawing multiple Winstons, \Nbut the problem Dialogue: 0,0:03:48.100,0:03:51.47,Default,,0000,0000,0000,,is they're all in the same place. Dialogue: 0,0:03:51.47,0:03:54.43,Default,,0000,0000,0000,,If we had x-ray vision,\Nwe could x-ray the canvas Dialogue: 0,0:03:54.43,0:03:57.63,Default,,0000,0000,0000,,and see three Winstons, \Nbut I don't have x-ray vision. Dialogue: 0,0:03:57.63,0:04:00.33,Default,,0000,0000,0000,,I don't know about you. Dialogue: 0,0:04:00.33,0:04:03.44,Default,,0000,0000,0000,,But, we can make a small change \Nto our function Dialogue: 0,0:04:03.44,0:04:05.49,Default,,0000,0000,0000,,that WILL make it obvious. Dialogue: 0,0:04:05.49,0:04:08.82,Default,,0000,0000,0000,,So you see faceX and faceY - \Nthey're always 202 and 208? Dialogue: 0,0:04:08.82,0:04:11.59,Default,,0000,0000,0000,,We can change this \Nto using the random() function - Dialogue: 0,0:04:11.59,0:04:14.85,Default,,0000,0000,0000,,- let's say like random() from 50 to 350 \Nand it'll generate Dialogue: 0,0:04:14.85,0:04:19.18,Default,,0000,0000,0000,,a random number between those two - \Nand we can do the same thing for here - Dialogue: 0,0:04:19.18,0:04:23.02,Default,,0000,0000,0000,,and so every time this function is called,\Nit generates this new random number, Dialogue: 0,0:04:23.02,0:04:26.57,Default,,0000,0000,0000,,and if we push restart, \Nwe can keep getting random Winstons. Dialogue: 0,0:04:26.57,0:04:29.36,Default,,0000,0000,0000,,So cool! \NWhoo!! Dialogue: 0,0:04:29.36,0:04:32.10,Default,,0000,0000,0000,,Alright - so I think this is cool \Nbecause it would have been Dialogue: 0,0:04:32.10,0:04:34.92,Default,,0000,0000,0000,,a lot of code to write this \Nif we didn't have it in a function. Dialogue: 0,0:04:34.92,0:04:38.32,Default,,0000,0000,0000,,It would have been\Nthree times the amount of code. Dialogue: 0,0:04:38.32,0:04:40.96,Default,,0000,0000,0000,,But it's still not AS useful \Nas it could be, Dialogue: 0,0:04:40.96,0:04:43.80,Default,,0000,0000,0000,,because we probably \Ndon't want random Winstons. Dialogue: 0,0:04:43.80,0:04:45.74,Default,,0000,0000,0000,,We probably want to be able \Nto position a Winston Dialogue: 0,0:04:45.74,0:04:47.85,Default,,0000,0000,0000,,at specific points on the screen. Dialogue: 0,0:04:47.85,0:04:49.94,Default,,0000,0000,0000,,So stay tuned, 'cause we'll talk \Nabout how pass parameters Dialogue: 0,0:04:49.94,0:04:52.48,Default,,0000,0000,0000,,to our functions next,\Nand then you'll be able Dialogue: 0,0:04:52.00,0:04:56.00,Default,,0000,0000,0000,,to do exactly that.