1 00:00:00,000 --> 00:00:00,320 2 00:00:00,320 --> 00:00:02,700 >> DAVID J. MALAN: Let's write a program in which we try to swap the values of 3 00:00:02,700 --> 00:00:04,970 two variables, x and y. 4 00:00:04,970 --> 00:00:07,490 In advance, I've written much of this program already. 5 00:00:07,490 --> 00:00:11,130 First, I declare a variable, x, and store in it the value, 1. 6 00:00:11,130 --> 00:00:14,120 Then I declare variable y and store in it the value, 2. 7 00:00:14,120 --> 00:00:17,700 >> Then I proceed with printf to display the values of those two variables. 8 00:00:17,700 --> 00:00:21,090 Then I claim with another printf to be swapping those variables. 9 00:00:21,090 --> 00:00:23,690 Then I call a function called swap. 10 00:00:23,690 --> 00:00:26,100 And then I claim that the variables have been swapped. 11 00:00:26,100 --> 00:00:30,610 And then I print out that what I claim are the new values of x and y. 12 00:00:30,610 --> 00:00:32,030 >> Now what is this function swap? 13 00:00:32,030 --> 00:00:34,970 It's not something that comes with C. Indeed, if we look at the top of my 14 00:00:34,970 --> 00:00:38,850 file, you'll notice that I've declared a prototype for this function swap 15 00:00:38,850 --> 00:00:42,750 specifying that it takes two ints, arbitrarily called a and b, and this 16 00:00:42,750 --> 00:00:44,300 function doesn't return anything. 17 00:00:44,300 --> 00:00:48,370 So presumably, it does what it says in swaps a's and b's values. 18 00:00:48,370 --> 00:00:50,170 Let's now implement swap. 19 00:00:50,170 --> 00:00:53,820 >> First, I'm going to declare a temporary variable and assign to it 20 00:00:53,820 --> 00:00:57,260 the value in a, though I could just as easily stored in it b. 21 00:00:57,260 --> 00:01:00,925 I'm then going to change the value of a to be equal to that in b. 22 00:01:00,925 --> 00:01:04,849 And then lastly, I'm going to change the value of b to be what a was but is 23 00:01:04,849 --> 00:01:06,340 now in temp. 24 00:01:06,340 --> 00:01:08,910 >> Now I claim what I've just done is logically correct. 25 00:01:08,910 --> 00:01:12,780 I've stored a in a temporary variable, changed a's value to be b's, and then 26 00:01:12,780 --> 00:01:15,580 changed b's value to be what a's was. 27 00:01:15,580 --> 00:01:18,620 But when I compile and run this program, I worry that's not 28 00:01:18,620 --> 00:01:20,140 going what I see. 29 00:01:20,140 --> 00:01:21,625 >> Make, no swap. 30 00:01:21,625 --> 00:01:24,350 dot slash, no swap. 31 00:01:24,350 --> 00:01:27,560 And unfortunately, according to printf, the value of x and y, even 32 00:01:27,560 --> 00:01:31,560 after we claim to be swapping them, is still 1 and 2. 33 00:01:31,560 --> 00:01:32,630 >> Now why is that? 34 00:01:32,630 --> 00:01:36,160 Well, it turns out that in main, when we call this function swap, passing in 35 00:01:36,160 --> 00:01:39,960 x and y as arguments, it's actually copies of x and y that 36 00:01:39,960 --> 00:01:41,310 are passed into swap. 37 00:01:41,310 --> 00:01:45,430 Namely, their values 1 and 2 are passed into swap as a and b. 38 00:01:45,430 --> 00:01:48,590 And it's a and b that we're ultimately swapping in main. 39 00:01:48,590 --> 00:01:50,810 >> That is, 1 becomes 2, 2 becomes 1. 40 00:01:50,810 --> 00:01:54,630 But because swap has no return value and because it received copies of x 41 00:01:54,630 --> 00:01:58,220 and y, it's behavior has no effect on x and y. 42 00:01:58,220 --> 00:02:02,610 Indeed, a and b are scoped, so to speak, to swap, whereas x and y remain 43 00:02:02,610 --> 00:02:03,990 scoped to main. 44 00:02:03,990 --> 00:02:05,070 >> So we have a problem. 45 00:02:05,070 --> 00:02:06,320 But how to fix it? 46 00:02:06,320 --> 00:02:08,312