1 00:00:00,790 --> 00:00:03,190 The following content is provided under a Creative 2 00:00:03,190 --> 00:00:04,730 Commons license. 3 00:00:04,730 --> 00:00:07,030 Your support will help MIT OpenCourseWare 4 00:00:07,030 --> 00:00:11,390 continue to offer high-quality educational resources for free. 5 00:00:11,390 --> 00:00:13,990 To make a donation or view additional materials 6 00:00:13,990 --> 00:00:17,880 from hundreds of MIT courses, visit MIT OpenCourseWare 7 00:00:17,880 --> 00:00:18,840 at ocw.mit.edu. 8 00:00:23,330 --> 00:00:26,590 ANA BELL: So here's some code. 9 00:00:26,590 --> 00:00:30,800 I'm defining a function named sq, 10 00:00:30,800 --> 00:00:33,410 and it takes in two parameters. 11 00:00:33,410 --> 00:00:37,580 I'm defining a function named f, takes in one parameter, 12 00:00:37,580 --> 00:00:40,460 and then I'm doing these two lines. 13 00:00:40,460 --> 00:00:43,640 The first one is just calling function sq, 14 00:00:43,640 --> 00:00:45,613 and the next one is just printing the value. 15 00:00:48,710 --> 00:00:53,080 So let's first see what-- nice. 16 00:00:53,080 --> 00:00:55,850 OK. 17 00:00:55,850 --> 00:00:57,000 Let's work through it. 18 00:01:01,491 --> 00:01:08,130 So The first things we see here is two function definitions, 19 00:01:08,130 --> 00:01:08,630 right? 20 00:01:08,630 --> 00:01:10,254 So we don't currently care about what's 21 00:01:10,254 --> 00:01:12,920 inside them right now because we haven't made a function 22 00:01:12,920 --> 00:01:14,100 call yet. 23 00:01:14,100 --> 00:01:16,880 So the first thing we do is we have the function 24 00:01:16,880 --> 00:01:21,250 call calc equal sq f and 2. 25 00:01:21,250 --> 00:01:22,070 OK? 26 00:01:22,070 --> 00:01:29,470 So inside sq, we're going to have func and x. 27 00:01:32,640 --> 00:01:35,700 And func is going to get mapped to f, 28 00:01:35,700 --> 00:01:39,670 and x is going to get mapped to 2, right? 29 00:01:39,670 --> 00:01:41,530 So we're taking the variables in order 30 00:01:41,530 --> 00:01:43,210 and mapping them to those. 31 00:01:43,210 --> 00:01:46,560 Func is f, and x is 2. 32 00:01:46,560 --> 00:01:48,690 First thing the function does, sq, 33 00:01:48,690 --> 00:01:52,690 is create this variable y is equal to x squared. 34 00:01:52,690 --> 00:01:56,145 So we're going to have y is equal to 4. 35 00:01:59,490 --> 00:02:02,970 And then we're going to return func y. 36 00:02:02,970 --> 00:02:07,980 So func of y, this is going to be-- we're 37 00:02:07,980 --> 00:02:14,980 just replacing the parameters f of 4, right? 38 00:02:14,980 --> 00:02:18,840 So now this is another function call. 39 00:02:18,840 --> 00:02:20,710 We know what f is. 40 00:02:20,710 --> 00:02:22,516 This program knows what f is. 41 00:02:22,516 --> 00:02:29,300 f is going to be this part right here, which returns x squared. 42 00:02:29,300 --> 00:02:30,250 OK? 43 00:02:30,250 --> 00:02:39,315 So in f, x gets mapped to whatever variable we put in, 44 00:02:39,315 --> 00:02:40,830 which in this case is 4. 45 00:02:45,450 --> 00:02:48,390 And we're going to return to whoever 46 00:02:48,390 --> 00:02:54,520 called us, which is over here, 4 squared, which is 16. 47 00:02:54,520 --> 00:02:55,330 OK? 48 00:02:55,330 --> 00:02:57,545 So f of 4 gets replaced with 16. 49 00:03:02,260 --> 00:03:06,870 And f of 4 was up here, right? 50 00:03:06,870 --> 00:03:09,040 We're just popping out of scopes now. 51 00:03:09,040 --> 00:03:11,090 So f of 4 was up here. 52 00:03:11,090 --> 00:03:17,710 So then that line there, return func y, is going to return 16. 53 00:03:17,710 --> 00:03:21,584 And whoever called us was down here, calc is equal to 16. 54 00:03:21,584 --> 00:03:23,000 And then we're just printing calc. 55 00:03:27,000 --> 00:03:29,990 So 16 was right. 56 00:03:29,990 --> 00:03:31,540 Yay.