1 00:00:07,880 --> 00:00:14,640 Welcome back, for a little way of the programmer segment on how to decode a function. 2 00:00:14,640 --> 00:00:17,700 Here's a habit that I hope you'll cultivate whenever 3 00:00:17,700 --> 00:00:21,720 you see a function definition or whenever you write one. 4 00:00:21,720 --> 00:00:24,780 Decode that function definition, 5 00:00:24,780 --> 00:00:27,705 ask yourself three questions; 6 00:00:27,705 --> 00:00:33,340 First, how many parameters? 7 00:00:39,860 --> 00:00:46,560 Second, what types and values will be bound to those parameter names? 8 00:00:50,830 --> 00:00:56,190 Third, what is the type of the return value? 9 00:01:04,160 --> 00:01:08,049 So, let's work through a few examples. 10 00:01:08,330 --> 00:01:11,744 Here's a function cyu3, 11 00:01:11,744 --> 00:01:13,965 how many parameters does it have? 12 00:01:13,965 --> 00:01:17,580 That's our first question whenever we try to decode a function, 13 00:01:17,580 --> 00:01:19,230 and it's the easiest one to answer, 14 00:01:19,230 --> 00:01:20,860 cause you can just look inside 15 00:01:20,860 --> 00:01:25,570 the parentheses and you can see that there are three variable names separated by commas. 16 00:01:25,570 --> 00:01:36,110 So, three inputs, and that's exactly the question that's being asked. 17 00:01:36,570 --> 00:01:41,345 Sure? Three inputs x, y, and z. 18 00:01:41,345 --> 00:01:43,770 The next question is, 19 00:01:43,770 --> 00:01:46,810 what will their types be? 20 00:01:48,920 --> 00:01:52,830 So, what are the types of x, y, and z? 21 00:01:52,830 --> 00:01:54,980 The first question is asking you is for x and y, 22 00:01:54,980 --> 00:01:57,100 cause it turns out they have to have the same type. 23 00:01:57,100 --> 00:01:58,675 How can you tell that? 24 00:01:58,675 --> 00:02:02,460 There's nothing in the function declaration that tells you that. 25 00:02:02,460 --> 00:02:06,590 So, you have to look in the code and see how those variables are used. 26 00:02:06,590 --> 00:02:11,685 In particular, we have x minus y in this expression. 27 00:02:11,685 --> 00:02:16,295 What are the types of objects that you can do a minus on? 28 00:02:16,295 --> 00:02:18,930 The answer is numbers. 29 00:02:18,930 --> 00:02:22,465 They could be integers, they could be floats, 30 00:02:22,465 --> 00:02:26,725 strings not so much, and not this either. 31 00:02:26,725 --> 00:02:34,035 Let's try that, integers and floats. 32 00:02:34,035 --> 00:02:37,125 Sure enough, we are correct. 33 00:02:37,125 --> 00:02:40,630 How about the type of z? 34 00:02:41,390 --> 00:02:44,370 Well, we have to look where z is used, 35 00:02:44,370 --> 00:02:49,095 and z is used down here in the else. 36 00:02:49,095 --> 00:02:51,675 We're running an append method on it. 37 00:02:51,675 --> 00:02:54,810 Which objects can you do append on? 38 00:02:54,810 --> 00:02:57,614 The answer is only lists. 39 00:02:57,614 --> 00:02:59,880 Remember that lists are mutable. 40 00:02:59,880 --> 00:03:03,465 You can do append, strings you're not allowed to. 41 00:03:03,465 --> 00:03:05,445 So, C is correct. 42 00:03:05,445 --> 00:03:08,405 If I had answered D, I get a little feedback telling me, 43 00:03:08,405 --> 00:03:11,880 "Append can't be performed on strings." 44 00:03:12,110 --> 00:03:15,890 Then the final question for decoding of a function is, 45 00:03:15,890 --> 00:03:19,740 what kind of return value does it give you? 46 00:03:19,740 --> 00:03:23,035 There are two spots in this code, 47 00:03:23,035 --> 00:03:28,565 where we're returning a value either y minus 2 or x plus 3. 48 00:03:28,565 --> 00:03:32,750 We previously inferred that both y and x had to be numbers, 49 00:03:32,750 --> 00:03:34,835 either integers or floats, 50 00:03:34,835 --> 00:03:40,880 and this doesn't tell us anymore about whether they're going to be integers or floats. 51 00:03:40,880 --> 00:03:44,345 Minus and plus are both operations that work on both of them, 52 00:03:44,345 --> 00:03:49,505 so the return value could either be an integer or a float. 53 00:03:49,505 --> 00:03:52,880 Your debugging sessions will be a lot shorter, 54 00:03:52,880 --> 00:03:57,610 if you can always answer those questions about any function that you're working with. 55 00:03:57,610 --> 00:03:59,015 So, build the habit. 56 00:03:59,015 --> 00:04:02,875 Whenever you see a function decoded by figuring out, 57 00:04:02,875 --> 00:04:04,680 how many input parameters? 58 00:04:04,680 --> 00:04:11,320 What are their types? What value will be returned? See you next time.