Transcript

The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high-quality educational resources for free. To make a donation or view additional materials from hundreds of MIT courses, visit MIT OpenCourseWare at ocw.mit.edu. So, here's um here's some code. I'm defining a function function named sq. And it takes in two parameters. I'm defining a function named f, takes in one parameter. And then I'm doing these two lines. The first one is just calling function sq. And the next one is just printing the value. So, let's first see what uh nice.

Okay. Uh let's work through it. So, the first things we see here is two function definitions, right? So, we don't currently care about what's inside them right now because we haven't made a function call yet. So, the first thing we do is we have the function call calc equal sq f and two. Okay? So, inside sq, we're going to have func and x. And func is going to get mapped to f and x is going to get mapped to two. Right? We're taking the variables in order and mapping them to those. So, f is func is f and x is two. First thing the function does, sq, is create this variable y is equal to x squared.

So, we're going to have y is equal to four. And then we're going to return funk y. So, funk of y, this is going to be we're just replacing the parameters f of four. Right? So, now this is another function call. We know what f is. Python This program knows what f is. F is going to be this part right here, which returns x squared. Okay? So, in f, x gets mapped to whatever variable we put in, which in this case is four. And we're going to return to whoever called us, which is over here, four squared, which is 16. Okay? So, f of four gets replaced with 16. And f of four was up here, right? We're just popping out of scopes now.

So, f of four was up here. So, then this that line there, return funk y, is going to return 16. And whoever called us was down here, calc is equal to 16. And then we're just printing calc. So, 16 was right. Yay.