WEBVTT

00:00:00.790 --> 00:00:03.190
The following content is
provided under a Creative

00:00:03.190 --> 00:00:04.730
Commons license.

00:00:04.730 --> 00:00:07.030
Your support will help
MIT OpenCourseWare

00:00:07.030 --> 00:00:11.390
continue to offer high-quality
educational resources for free.

00:00:11.390 --> 00:00:13.990
To make a donation or
view additional materials

00:00:13.990 --> 00:00:17.880
from hundreds of MIT courses,
visit MIT OpenCourseWare

00:00:17.880 --> 00:00:18.840
at ocw.mit.edu.

00:00:23.330 --> 00:00:26.590
ANA BELL: So here's some code.

00:00:26.590 --> 00:00:30.800
I'm defining a
function named sq,

00:00:30.800 --> 00:00:33.410
and it takes in two parameters.

00:00:33.410 --> 00:00:37.580
I'm defining a function named
f, takes in one parameter,

00:00:37.580 --> 00:00:40.460
and then I'm doing
these two lines.

00:00:40.460 --> 00:00:43.640
The first one is just
calling function sq,

00:00:43.640 --> 00:00:45.613
and the next one is
just printing the value.

00:00:48.710 --> 00:00:53.080
So let's first see what-- nice.

00:00:53.080 --> 00:00:55.850
OK.

00:00:55.850 --> 00:00:57.000
Let's work through it.

00:01:01.491 --> 00:01:08.130
So The first things we see here
is two function definitions,

00:01:08.130 --> 00:01:08.630
right?

00:01:08.630 --> 00:01:10.254
So we don't currently
care about what's

00:01:10.254 --> 00:01:12.920
inside them right now because
we haven't made a function

00:01:12.920 --> 00:01:14.100
call yet.

00:01:14.100 --> 00:01:16.880
So the first thing we do
is we have the function

00:01:16.880 --> 00:01:21.250
call calc equal sq f and 2.

00:01:21.250 --> 00:01:22.070
OK?

00:01:22.070 --> 00:01:29.470
So inside sq, we're
going to have func and x.

00:01:32.640 --> 00:01:35.700
And func is going
to get mapped to f,

00:01:35.700 --> 00:01:39.670
and x is going to get
mapped to 2, right?

00:01:39.670 --> 00:01:41.530
So we're taking the
variables in order

00:01:41.530 --> 00:01:43.210
and mapping them to those.

00:01:43.210 --> 00:01:46.560
Func is f, and x is 2.

00:01:46.560 --> 00:01:48.690
First thing the
function does, sq,

00:01:48.690 --> 00:01:52.690
is create this variable
y is equal to x squared.

00:01:52.690 --> 00:01:56.145
So we're going to
have y is equal to 4.

00:01:59.490 --> 00:02:02.970
And then we're going
to return func y.

00:02:02.970 --> 00:02:07.980
So func of y, this is
going to be-- we're

00:02:07.980 --> 00:02:14.980
just replacing the
parameters f of 4, right?

00:02:14.980 --> 00:02:18.840
So now this is
another function call.

00:02:18.840 --> 00:02:20.710
We know what f is.

00:02:20.710 --> 00:02:22.516
This program knows what f is.

00:02:22.516 --> 00:02:29.300
f is going to be this part right
here, which returns x squared.

00:02:29.300 --> 00:02:30.250
OK?

00:02:30.250 --> 00:02:39.315
So in f, x gets mapped to
whatever variable we put in,

00:02:39.315 --> 00:02:40.830
which in this case is 4.

00:02:45.450 --> 00:02:48.390
And we're going to
return to whoever

00:02:48.390 --> 00:02:54.520
called us, which is over
here, 4 squared, which is 16.

00:02:54.520 --> 00:02:55.330
OK?

00:02:55.330 --> 00:02:57.545
So f of 4 gets replaced with 16.

00:03:02.260 --> 00:03:06.870
And f of 4 was up here, right?

00:03:06.870 --> 00:03:09.040
We're just popping
out of scopes now.

00:03:09.040 --> 00:03:11.090
So f of 4 was up here.

00:03:11.090 --> 00:03:17.710
So then that line there, return
func y, is going to return 16.

00:03:17.710 --> 00:03:21.584
And whoever called us was down
here, calc is equal to 16.

00:03:21.584 --> 00:03:23.000
And then we're
just printing calc.

00:03:27.000 --> 00:03:29.990
So 16 was right.

00:03:29.990 --> 00:03:31.540
Yay.