 We have another question on the midterm two from fall 2015 and we're going over specifically the problem on pass by name. So here we have a snippet of c code and we're saying if this c code, so we read the question, say if parameters are passed by name then the output of this program is something. So one thing we'll see this is problem five continued so we're not going to go over it but there was a problem five that had exactly the same code. And here we have if parameters are passed by value, then the output of the program is something and here it's passed by reference the output of something. So a meta test taking strategy is if I'm asked, if you're being asked to do three different things, they're probably going to be different but maybe not necessarily. Right. So that would be one way to check yourself as you're doing this. Like why would I ask you to just keep doing the same thing over and over again if it outputs the same thing. Although, you know, now that I say that, of course, the trick would be to do the opposite right to throw you off. But this is an important thing. The other important thing from here that I recall from grading, these are dashes. If we ask the output of this program is and you put output that separated by spaces or separated by new lines, that is not the output of the program. That's a super easy way to lose points that it should not be lost. Oh dashes. Yes, the dash is inside here. So technically it should be like this dash to that dash dash. It should be a dash star b and then c zero dash c one dash c two. I mean, we haven't gone over what it is, but I expect you to be able to understand and read these printf. Cool. Okay, so where do we start? No, where do we start in looking at this program? Oh, in Maine. In Maine, yes. We start here. Cool. So if parameters are passed by name, the output of this program is blank. And by the time you've probably got to this program, you've probably been going over this code. So you have a decent understanding of it. But we haven't done that yet. So here, we have a local x is value 10. And if we really wanted to do this, we could actually create a same type of stack situation like we did before. And that would help us keep track of different global variables versus local variables. I'm just kind of going to do this in here in the program as it's executing. But that's definitely something you can do with your scratch paper. Okay. Okay. So then we say it wise equal to malloc size of int. Right. The other way to think about this is I could also do box circle diagrams here. So I could say x. And since I didn't realize there's pointers, since there's pointers, that would be helpful. So we say y is equal to malloc size of int. So y now has something, let's say alpha in it. So we know there exists something that's where? Where's this box, this memory box located on the heat? Yes. Trick questions. Not trick questions, but okay, there's nothing in it. So we don't know what's in there, but we know this is what y looks like. So when we say star y is equal to 20. So how does that change my diagram? Top right becomes 20. Sweet. Now I call foo of x y. Right. So what does that mean? So yeah, the important thing is I'm passed by name. Right. So I'm going to start executing. Now I've gone all the way up to here. Now I'm going to start executing here. Right. So we're going to have A and B. So I have A here. What does it refer to? So I meant, what kind of semantics am I in? Well, that's actually the wrong way to think about it. But this will kind of help us. So A and B are parameters. So we have to kind of treat them differently in some sense. Right. But we know in this invocation that A and B are x and y from foo. So we can basically think about what if this entire function was replaced in here with all the a's in here being replaced with this x and all the b's in here being replaced with this y. Yes. But right. So we will see as we get into another function that we have to be careful about how we do this. So here we have a local C. C is an array, which means it points to some other value beta, which has the value I'm just going to do like a zero, one, two, something like this. Cool. And this is that beta. Then it has a local I whose value is zero. And we say A is equal to I. So what does this mean? Take the value in the location associated with I and copy it where? Yes, within x. So it's just like as if this x over here was here. So we're saying essentially x equals I. The important rule is that this x scope is decided here. So we we know this x is bound to here this way when we change when we set this I we will change this to what was it zero. Right. Yeah, it's kind of and also not because you're not resolved, you know, you still know this a refers to this a, but and you know that this x when you invoke this refers to this x. So all the a's in here will now refer to this x. But if there was a local x, it would not refer to that. Well, yes, you can do that. It's a little tricky because you can make mistakes when you copy it over. Right. But yes, that's a way to think about it. Okay, so then we get here and we have B. So we're saying B is equal to malloc size of int. So B is why so this is exactly the same thing as up here doing y equals malloc size of int. Right. So how does that change? We got to create a new memory block. I'm running out of Greek letters. We'll call it delta skip whatever C is. I don't know what's inside of it yet. But now I know that why will not have the value alpha it will have now delta. So when I was here, now I get here, I do star B is equal to 42. So B. So now delta will be the value 42. And now I'm saying C two is equal to bar of ICI. Okay, this is where it gets tricky. So I will eventually be changing whatever C two is right but not until bar returns. Right. I have to call function bar. It's going to return me a result and I will put that at C two. Great. So then let's look at function bar. So function bar has local variables A and B. These have they have no relation to the A and B that are parameters to foo. Right. Because they're in their own scope. Absolutely no reference. Cool. So we're passed by names. We don't create new values here. So we have a local I and into an I in bar and that will obviously be different than this I here. So I say I is equal to A plus B. So what happens is now when I see this A and this B, it's as if I'm doing I plus CI because these are the parameters. So it's like putting their names there, but the scope still maps to here. So I is what at this point? Zero. Zero. Right. This I and this is the important thing we're setting a local I equal to the value of this I but this I will always refer to here. So this is this I is zero. And so what's C of this I zero. So this I is going to be what? Zero. Yes. Different I right. This is this is the I in bar and this is the I in foo. Right. The scoping rule. Great. So we just did this line. Now we say A is equal to A plus one. Right. So we first need the what's A and A from our call here is I. And remember this is I foo. So we say, okay, take zero in I foo. Add one to it and set it back into I foo. Right. Pass by name. Oops. Sorry. Did that. Now we're going to return I plus B. B is C I. Which I is this? I from this is the I from foo. Right in here because this scoping rules mean this I maps here. But now we have this B and we have, let me open that. Now we have this B. Now this I, what does this I refer to? Hi, we're recording something right now. So this I, what does this I refer to? The return I part? Yes. This I right here. That should be I from bar. Yes. This is I bar. Right. This is the local I. Cool. So we're going to take B. So what's C I now? C of one. C of one, which is one. Right. And then we add that to I, which is I bar, which is one. And we return that from bar. And then we're setting C two equal to one. Right. Right. Now we print out stuff. So now we got to make, now this is basically we've done all the work. So we can get to the print outputs so that we can know that we did it right. And that's all with all the credit, right? The print part. Yes. So we print out A. So A is X, which is zero. Zero dash hyphen star B. So what's B? Is Y and D referencing Y is 42. And what's this character? A new line. Yes. So now we're going to start writing from here because we just have to put a new line. We don't have to write slash N. I mean, I guess you could do that, but please don't. Just write it on another line. Now we have another print out. We're printing out C zero dash C one dash C two. So what are they? Oh, that's zero dash one dash one. Yep. Zero one one. And then just like, then we finish. We return from foo. We then go down here and we print out what? Star X. Just X and star Y. Zero and 42. And then we return X and then we're done. So my question is on X. Okay, that's good. Yeah, this is right. And this is different than the other ones, right? Okay, it should be. The other answers on the other ones. Yes. Oh, that was a lot of detail tracking. Like we didn't make a mistake on a friend. Will we get any credit for it? If we show our work like that, maybe a bunch of credit or something? Nope. Okay, good. Probably not. Because this shows that you know, I mean, you know, you're testing a lot of things in here, but at this point, you should be able to, you know, just like the previous example, right, that you can understand the program execution, you can step through it line by line, you can reason about what it's doing. I mean, this is why we spent so much time talking about semantics, like what, what do these things actually mean so that you can know and be able to do this. And then on top of that, then you show me, you know, pass by name, because you're doing the pass by name part correctly. Cool. All right. Say goodbye, everyone.