1 00:00:07,940 --> 00:00:11,670 Welcome back. There's one more tricky thing 2 00:00:11,670 --> 00:00:14,520 that we want you to be able to reason about with functions, 3 00:00:14,520 --> 00:00:16,465 it's called side effects. 4 00:00:16,465 --> 00:00:21,115 If a function makes a change to a mutable object like a list or a dictionary, 5 00:00:21,115 --> 00:00:22,945 that's called a side effect. 6 00:00:22,945 --> 00:00:27,710 The vocabulary here is that the main effect of a function is the value it returns, 7 00:00:27,710 --> 00:00:31,615 and any other lasting impact that it has is a side effect. 8 00:00:31,615 --> 00:00:34,340 One other thing that I'll refer to as 9 00:00:34,340 --> 00:00:37,885 a side effect is printing something out in the Output window. 10 00:00:37,885 --> 00:00:41,375 Let's see a side effect of mutating a list. 11 00:00:41,375 --> 00:00:47,830 First, this code gives us a little reminder that variables are local. 12 00:00:49,010 --> 00:00:52,110 So, we create these two functions. 13 00:00:52,110 --> 00:00:54,375 We make a local variable y, 14 00:00:54,375 --> 00:00:56,190 that's in the global frame. 15 00:00:56,190 --> 00:00:58,940 Then we invoke the double function. 16 00:00:58,940 --> 00:01:04,180 The double function takes as input some value, 17 00:01:04,180 --> 00:01:07,240 which it assigns to a formal parameter y. 18 00:01:07,240 --> 00:01:12,230 So, notice that, we have a different y. 19 00:01:12,290 --> 00:01:14,650 They happened to have the same value, 20 00:01:14,650 --> 00:01:18,115 but they are completely different variables here. 21 00:01:18,115 --> 00:01:22,245 Then we're going to assign on line two of the code, 22 00:01:22,245 --> 00:01:24,685 a new value for y. 23 00:01:24,685 --> 00:01:27,820 Watch what happens in the double-stack frame. 24 00:01:27,820 --> 00:01:31,690 It's not going to affect what happens in the global frame. 25 00:01:31,690 --> 00:01:39,810 So, when we execute line two in the double frame, 26 00:01:39,810 --> 00:01:42,520 y now has the value 10. 27 00:01:44,250 --> 00:01:49,640 That did not have any effect in the global frame. 28 00:01:50,400 --> 00:01:57,055 You might also have noticed that the double function doesn't have a return statement, 29 00:01:57,055 --> 00:02:00,355 and therefore, it returns the value none. 30 00:02:00,355 --> 00:02:02,560 It doesn't actually matter in this case, 31 00:02:02,560 --> 00:02:07,200 because we don't do anything with the value of double, 32 00:02:07,200 --> 00:02:09,610 we're not assigning it to anything. 33 00:02:10,340 --> 00:02:15,835 When we do finally get to line 10 and we're going to print out the value y. 34 00:02:15,835 --> 00:02:19,205 We get the value y from the global frame. 35 00:02:19,205 --> 00:02:22,020 So, it's the value five that's going to print out, 36 00:02:22,020 --> 00:02:27,140 that the 10 that we had in the local frame for the double function. 37 00:02:29,040 --> 00:02:32,330 Sure enough, we get the value five. 38 00:02:32,330 --> 00:02:36,610 That was just a reminder that we have local variables, 39 00:02:36,610 --> 00:02:40,495 and changing a local variable doesn't affect the global variable. 40 00:02:40,495 --> 00:02:46,840 But our next lesson is that even though we don't affect a global variable, 41 00:02:46,840 --> 00:02:53,605 we might affect a value that is shared by a local variable and a global variable. 42 00:02:53,605 --> 00:02:55,765 So, variables are local, 43 00:02:55,765 --> 00:02:57,500 but objects are not. 44 00:02:57,500 --> 00:03:02,470 Here we're going to see that we mutate an object inside a function and it stays mutated. 45 00:03:02,470 --> 00:03:08,500 On line number 12, we've created a list with four elements, our students. 46 00:03:08,500 --> 00:03:11,940 On line 12, we have created a list called mylst. 47 00:03:11,940 --> 00:03:14,175 Its value is this list of four strings, 48 00:03:14,175 --> 00:03:18,040 "Our students are awesome," that's you. 49 00:03:18,080 --> 00:03:21,610 Then we're calling change it. 50 00:03:21,770 --> 00:03:25,485 On line 13, we call change it. 51 00:03:25,485 --> 00:03:30,795 That creates a new stack frame for the change it function, 52 00:03:30,795 --> 00:03:33,270 and its formal parameter, 53 00:03:33,270 --> 00:03:36,090 LST gets a value. 54 00:03:36,090 --> 00:03:41,250 The value it gets is whatever mylst had as its value. 55 00:03:41,250 --> 00:03:44,985 Well, mylst was pointing to that list object. 56 00:03:44,985 --> 00:03:50,285 So, the LST variable in the change its stack frame, 57 00:03:50,285 --> 00:03:54,390 that LST variable also points to the same list. 58 00:03:54,530 --> 00:03:57,240 This is important because, 59 00:03:57,240 --> 00:03:59,850 now when we get to line number five, 60 00:03:59,850 --> 00:04:02,505 list square bracket zero gets a different value. 61 00:04:02,505 --> 00:04:05,850 Instead of being hour it becomes Michigan, 62 00:04:05,850 --> 00:04:10,500 and instead of students we get wolverines. 63 00:04:10,500 --> 00:04:14,720 So, notice that, mylst and lst, 64 00:04:14,720 --> 00:04:19,685 these are still two different variables but because they're aliases for the same list, 65 00:04:19,685 --> 00:04:25,185 when we finish this execution and then we print mylst, 66 00:04:25,185 --> 00:04:28,590 mylst has the mutated value. 67 00:04:28,590 --> 00:04:32,710 The variable mylst is pointing to the object which has been mutated, 68 00:04:32,710 --> 00:04:35,680 and we get, "Michigan wolverines are awesome," instead of, 69 00:04:35,680 --> 00:04:37,895 "Our students are awesome." 70 00:04:37,895 --> 00:04:40,680 So, we call this a side effect. 71 00:04:40,680 --> 00:04:45,480 The change that function is having a side effect on the list object. 72 00:04:45,480 --> 00:04:48,820 Just as we talked about earlier when we first introduced 73 00:04:48,820 --> 00:04:51,890 the idea of multiple aliases for the same list object, 74 00:04:51,890 --> 00:04:54,715 this can get confusing if you're not careful. 75 00:04:54,715 --> 00:04:59,050 Sometimes it's clear that a function is going to have side effects and you expect it, 76 00:04:59,050 --> 00:05:02,485 but sometimes you'll be surprised and debugging can get difficult. 77 00:05:02,485 --> 00:05:05,360 What happened to mylst? You'll ask. 78 00:05:05,360 --> 00:05:07,724 To avoid potential confusion, 79 00:05:07,724 --> 00:05:11,800 it's best to just avoid side effects in your functions whenever you can. 80 00:05:11,800 --> 00:05:14,995 If functions never ever have side effects, 81 00:05:14,995 --> 00:05:17,515 that's a style called functional programming. 82 00:05:17,515 --> 00:05:20,570 There are programming languages built around that principle 83 00:05:20,570 --> 00:05:23,845 of functional programming but Python is more flexible, 84 00:05:23,845 --> 00:05:27,770 and we will sometimes make use of side effects but you should do 85 00:05:27,770 --> 00:05:32,490 it sparingly and consciously. See you next time.