1 00:00:06,360 --> 00:00:09,580 In this video, I'm going to talk about functions. 2 00:00:09,580 --> 00:00:13,440 Functions are the mechanism in the beginning student language that's going 3 00:00:13,440 --> 00:00:16,360 to let you write programs that let you produce a different value each time they 4 00:00:16,360 --> 00:00:18,330 run. Right now our programs are always 5 00:00:18,330 --> 00:00:22,760 producing the same value. So functions are incredibly important and 6 00:00:22,760 --> 00:00:25,240 powerful, and they're going to form the bulk of our programs. 7 00:00:26,570 --> 00:00:29,210 But I think what you're going to see here in a few minutes is that in the beginning 8 00:00:29,210 --> 00:00:32,570 student language, functions are not too difficult to learn. 9 00:00:34,360 --> 00:00:36,805 We're starting to be able to build up some interesting results. 10 00:00:36,805 --> 00:00:43,399 For example, here, by using above and circle three times, I can write an 11 00:00:43,399 --> 00:00:47,700 expression which when I run it, produces this little traffic light or at least 12 00:00:47,700 --> 00:00:51,010 it's sort of like a North American traffic light except that too many lights 13 00:00:51,010 --> 00:00:55,680 are on. Well let's look at this expression a bit. 14 00:00:55,680 --> 00:00:57,939 It's easy to be bothered by the amount of redundancy in it. 15 00:00:59,230 --> 00:01:03,850 Notice that in this expression, all of these parts are unchanging in the three 16 00:01:03,850 --> 00:01:06,220 calls to circle. These parts are unchanging. 17 00:01:07,520 --> 00:01:11,170 Only this part here is changing in each of the calls to circle. 18 00:01:11,170 --> 00:01:17,559 And it's easy to be worried, gosh, if programs get big, and a lot of them are 19 00:01:17,559 --> 00:01:22,400 sort of duplicated code like this That, that seems needless. 20 00:01:22,400 --> 00:01:26,770 That seems like it's going to be cumbersome, and it would be cumbersome, 21 00:01:26,770 --> 00:01:28,920 and it is needless. There's a mechanism that's going to let 22 00:01:28,920 --> 00:01:31,310 us prevent that called function definitions. 23 00:01:32,780 --> 00:01:35,560 So, what are function definitions, and how do they work? 24 00:01:35,560 --> 00:01:38,690 Well, you actually already understand a lot about function definitions. 25 00:01:38,690 --> 00:01:42,250 You just have to remember your grade school math in which you learned that you 26 00:01:42,250 --> 00:01:44,300 could write mathematical functions like this. 27 00:01:44,300 --> 00:01:50,140 For example, here is f of x. It equals 2 times x, and that means that 28 00:01:50,140 --> 00:01:56,870 f of 2, is 2 times 2, is 4, or f of 6, is 2 times 6, is 12. 29 00:01:56,870 --> 00:01:59,690 What's going on in these function definitions is that x, which is called 30 00:01:59,690 --> 00:02:02,050 the parameter, stands for the varying value. 31 00:02:02,050 --> 00:02:07,090 And then, the rest of the function definition is the unchanging value, the 2 32 00:02:07,090 --> 00:02:11,170 times in this case. And then the function can be used any 33 00:02:11,170 --> 00:02:16,310 number of times with a different value each time, or even in sometimes, it can 34 00:02:16,310 --> 00:02:21,960 be used with the same value as before. And each time the parameter will take on 35 00:02:21,960 --> 00:02:27,460 the changing value and you get the result of the function with that value. 36 00:02:27,460 --> 00:02:31,060 So how's this going to work for us. The beginning of student language has a 37 00:02:31,060 --> 00:02:34,469 function definition mechanism, and it works like this. 38 00:02:34,469 --> 00:02:38,158 I'm going to write define, open parentheses, the name of a function. 39 00:02:38,158 --> 00:02:43,100 In our functions, we're not going to have to use th-, single-character names likes 40 00:02:43,100 --> 00:02:45,920 x. I'm going to call this function bulb, and 41 00:02:45,920 --> 00:02:48,450 the name of a parameter, which in this case, I'll call: c. 42 00:02:48,450 --> 00:02:58,220 And then I'm going to write that the body of the function is: (circle 40 "solid", 43 00:02:58,220 --> 00:03:02,260 and then: c. What's going to happen here, just like in 44 00:03:02,260 --> 00:03:05,590 the mathematical functions, is that the parameter C is going to stand for the 45 00:03:05,590 --> 00:03:08,930 changing value. And then the body of the function is 46 00:03:08,930 --> 00:03:12,650 going to use that parameter in the place where it wants to use the changing value. 47 00:03:12,650 --> 00:03:18,350 And now I can show you how this function works if I write open paren bulb and then 48 00:03:18,350 --> 00:03:21,728 say purple. This is a call to the function. 49 00:03:21,728 --> 00:03:26,690 Intuitively you could think of it that the body of the function was evaluated 50 00:03:26,690 --> 00:03:32,080 With C taking on the changing values. So with C taking on the value purple. 51 00:03:32,080 --> 00:03:35,740 In a minute, I'll show you the exact evaluation rules, but for now you can 52 00:03:35,740 --> 00:03:40,310 think of it intuitively that way. Now that I have the bulb function, I can 53 00:03:40,310 --> 00:03:44,359 clean this up some. Let me just take this whole thing, and 54 00:03:44,359 --> 00:03:49,645 for now, I'll just comment it out. I'm going to get rid of this bulb purple, 55 00:03:49,645 --> 00:03:52,730 because that one's just for demonstration, and I'll rewrite the 56 00:03:52,730 --> 00:03:58,094 original expression we had this way. And then when I run the program, I get 57 00:03:58,094 --> 00:04:02,982 the same result as before. Now, this expression, at the end, is much 58 00:04:02,982 --> 00:04:06,450 more concise than the original expression. 59 00:04:06,450 --> 00:04:10,430 It reduces the duplication And that's what makes it more concise. 60 00:04:10,430 --> 00:04:14,620 And also by choosing a nice name here, by choosing the name bulb, I give the code a 61 00:04:14,620 --> 00:04:17,059 bit more meaning. I can now think of these as three bulbs, 62 00:04:17,059 --> 00:04:20,870 one atop the other. And what we do now in a real program is 63 00:04:20,870 --> 00:04:25,409 we go ahead and completely get rid of this plummeted-out stuff. 64 00:04:25,409 --> 00:04:29,960 And just because I think it looks better because These bulbs are one on top the 65 00:04:29,960 --> 00:04:32,490 other. I would probably format this above that 66 00:04:32,490 --> 00:04:35,000 way. And now I've got something that's nice 67 00:04:35,000 --> 00:04:38,307 and concise and it's clear for me to understand what it's doing. 68 00:04:38,307 --> 00:04:43,900 What I want to do now is the same thing we've done with all the other beginnings 69 00:04:43,900 --> 00:04:47,560 student language constructs we've seen, which is to go over in a little bit more 70 00:04:47,560 --> 00:04:51,720 detail the rules for forming function definitions and function calls and also 71 00:04:51,720 --> 00:04:57,314 the rules for evaluating function calls. Before I get to those rules, I just want 72 00:04:57,314 --> 00:05:01,229 to show you something which is where you could find all of the rules for the VSL 73 00:05:01,229 --> 00:05:04,711 language. If you go to the course web page and you 74 00:05:04,711 --> 00:05:09,890 scroll here to the languages tab, then this page has all the rules for the BSL 75 00:05:09,890 --> 00:05:13,780 language including how to form all the kinds of expressions we've seen and the 76 00:05:13,780 --> 00:05:17,860 rules for evaluating them and so on. The rule for forming a function 77 00:05:17,860 --> 00:05:22,020 definition is simple. You start with open paren define because 78 00:05:22,020 --> 00:05:25,710 it's a definition. And then we have open paren and the name 79 00:05:25,710 --> 00:05:30,270 of the function, followed by the names of the parameters. 80 00:05:30,270 --> 00:05:33,585 In the example we have so far we just have one parameter, c. 81 00:05:33,585 --> 00:05:38,780 But we'll define other functions later that have more than one parameter, and 82 00:05:38,780 --> 00:05:44,260 then we close that paren so this first part has open paren the name of the 83 00:05:44,260 --> 00:05:47,990 function the name of the parameters, then we close that, that paren. 84 00:05:47,990 --> 00:05:52,300 And then we always put it on a new line. We have an expression which forms the 85 00:05:52,300 --> 00:05:56,454 body of the function. So here's the example we've seen so far, 86 00:05:56,454 --> 00:06:02,260 define, the name of the function is bulb, the parameter is C and here's the body of 87 00:06:02,260 --> 00:06:05,880 the function. To form a function call expression, we 88 00:06:05,880 --> 00:06:11,640 put open paren, the name of a defined function, and then we put some number of 89 00:06:11,640 --> 00:06:16,500 expressions corresponding to the number of parameters the function has. 90 00:06:16,500 --> 00:06:20,490 So for our bulb function, which has a single parameter, we put a single 91 00:06:20,490 --> 00:06:24,632 expression after the bulb name. And those expressions are all called 92 00:06:24,632 --> 00:06:28,520 operands. To show you how the rules for evaluation 93 00:06:28,520 --> 00:06:31,640 related to functions work, you're going to first start by getting a 94 00:06:31,640 --> 00:06:35,641 somewhat simpler example. What I've got here is a call to bulb 95 00:06:35,641 --> 00:06:41,482 where its argument, instead of already being a value is going to be another 96 00:06:41,482 --> 00:06:46,432 call, a primitive call. And I'm mostly going to set up by putting 97 00:06:46,432 --> 00:06:53,370 the Rules for evaluating functions and permitive calls here on the right. 98 00:06:53,370 --> 00:06:57,210 So let's see, we look at open paren bulb, and that tells us that we've got a 99 00:06:57,210 --> 00:07:01,870 function call. So the rule for evaluating function call 100 00:07:01,870 --> 00:07:07,948 is to first reduce the operands to values. 101 00:07:07,948 --> 00:07:11,121 So now let's go look at the first operand. 102 00:07:11,121 --> 00:07:20,424 First operand is open paren string append, so that's a primative call. 103 00:07:20,424 --> 00:07:23,710 So, we now look at the rules for evaluating a primitive call. 104 00:07:23,710 --> 00:07:28,570 And the first rule is to reduce operands to values again. 105 00:07:28,570 --> 00:07:32,700 Notice that the rule, the first rule for function calls and operand, primitive 106 00:07:32,700 --> 00:07:34,960 calls is the same, you reduce the operands to values. 107 00:07:34,960 --> 00:07:40,880 Well, let's see. RR, the string re and the string Stream D 108 00:07:40,880 --> 00:07:46,220 are both already values, so now we can apply the string of pan primitive. 109 00:07:46,220 --> 00:07:53,460 And the first full step in the evaluation is that we now have bulb with an operand 110 00:07:53,460 --> 00:07:59,742 of the value web. So now going back to the function call 111 00:07:59,742 --> 00:08:04,719 roles, we have now reduced the first operand to a value. 112 00:08:04,719 --> 00:08:09,610 We would, so we reduce all operands to a value, and now we're going to replace the 113 00:08:09,610 --> 00:08:13,860 function call by the body of the function. 114 00:08:13,860 --> 00:08:19,600 So I've copied the body of function down here, but in the body of function, we're 115 00:08:19,600 --> 00:08:24,809 going to replace every reoccurrence of the parameter c by the argument, the 116 00:08:24,809 --> 00:08:32,300 corresponding argument to the function in this case, red. 117 00:08:32,300 --> 00:08:35,380 And now what do we have? Well, open paren circle, this is a call 118 00:08:35,380 --> 00:08:40,290 to a primitive. So notice the function call is completely 119 00:08:40,290 --> 00:08:44,610 gone now, once we've replaced the call with its body, we just work on evaluating 120 00:08:44,610 --> 00:08:48,660 the replaced body. This is a call to a primitive. 121 00:08:48,660 --> 00:08:52,560 The first rule for a primitive is to reduce all the operands to values. 122 00:08:52,560 --> 00:08:57,360 There's three operands, in this case, and they are all already values. 123 00:08:57,360 --> 00:09:04,180 So now the second step of a call to a primitive is to just apply the primitive 124 00:09:04,180 --> 00:09:09,020 to the values. And the value of that primitive call is a 125 00:09:09,020 --> 00:09:11,650 red circle. And now all we've got is a value, so 126 00:09:11,650 --> 00:09:18,210 evaluation stops. Let me just point one thing out to you 127 00:09:18,210 --> 00:09:21,640 about the way I've done this and step by step evaluation. 128 00:09:21,640 --> 00:09:26,750 By writing all the different steps to the evaluation in the definitions window, 129 00:09:26,750 --> 00:09:30,800 this way, what I can do now is actually run this. 130 00:09:30,800 --> 00:09:36,030 And since there's four steps to the evaluation- I should see the same result 131 00:09:36,030 --> 00:09:40,630 four times because when rackets starts here and runs to the end, it gets a red 132 00:09:40,630 --> 00:09:42,650 circle. When it starts here and runs to the end, 133 00:09:42,650 --> 00:09:45,140 it gets a red circle. When it starts here and runs to the end, 134 00:09:45,140 --> 00:09:47,720 it gets a red circle. And when it starts with a red circle and 135 00:09:47,720 --> 00:09:52,861 runs to the end, it gets a red circle. So if you do your step-by-step evaluation 136 00:09:52,861 --> 00:09:58,510 exercises, in the definitions window, like this, and then run it, you can kind 137 00:09:58,510 --> 00:10:01,360 of check whether you've got the right step by step evaluation because you 138 00:10:01,360 --> 00:10:04,744 should see the same value repeated each time. 139 00:10:04,744 --> 00:10:11,230 Now you understand how to define functions, and you know the rules for 140 00:10:11,230 --> 00:10:15,220 evaluating function calls. As I said at the beginning, functions are 141 00:10:15,220 --> 00:10:18,540 going to be incredibly important to all of our racket programs. 142 00:10:18,540 --> 00:10:21,690 So it's really important that you understand how they work. 143 00:10:21,690 --> 00:10:26,260 So if it's ever unclear to you, just come back to this video, and also be sure to 144 00:10:26,260 --> 00:10:29,500 work the practice problems that are associated with this video. 145 00:10:30,600 --> 00:10:36,110 Just as an aside, so you understand how powerful functions are, one of the most 146 00:10:36,110 --> 00:10:40,780 important theoretical results in computer science says that if you have a language 147 00:10:40,780 --> 00:10:45,680 that just has functions, just functions, no strings, no numbers, just functions, 148 00:10:45,680 --> 00:10:49,440 you can actually write any program that can be written in any language. 149 00:10:49,440 --> 00:10:52,700 Now, you know that's a theoretical result that makes academics happy. 150 00:10:52,700 --> 00:10:56,380 A language like that would be pretty cumbersome to program in, so you would 151 00:10:56,380 --> 00:11:01,420 never do it. Back in practical terms now we want 152 00:11:01,420 --> 00:11:04,780 functions, we want strings, we want images, we want numbers, we want all of 153 00:11:04,780 --> 00:11:08,520 that. Even so, functions are pretty essential 154 00:11:08,520 --> 00:11:09,210 to the whole game.