1 00:00:06,600 --> 00:00:10,200 - It's quite common to return a structure from a function. 2 00:00:10,200 --> 00:00:14,370 It lets you return a lot of data as a cohesive bundle 3 00:00:14,370 --> 00:00:17,190 in a single object and that can be useful 4 00:00:17,190 --> 00:00:18,420 to the calling function. 5 00:00:18,420 --> 00:00:22,500 You return a lot of data back to the caller in one object, 6 00:00:22,500 --> 00:00:25,421 they can then use that data as they like. 7 00:00:25,421 --> 00:00:28,320 So there were two ways to return a structure 8 00:00:28,320 --> 00:00:29,730 from a function. 9 00:00:29,730 --> 00:00:31,320 You can either return by value, 10 00:00:31,320 --> 00:00:32,970 which we'll have a look at now, 11 00:00:32,970 --> 00:00:34,830 or you can return by reference, 12 00:00:34,830 --> 00:00:37,530 which we'll have a look at a bit later. 13 00:00:37,530 --> 00:00:39,660 So have a look at this function. 14 00:00:39,660 --> 00:00:41,370 It's called build_employee. 15 00:00:41,370 --> 00:00:42,983 I've given it name build_employee, 16 00:00:43,830 --> 00:00:48,180 and it takes some parameters as input. 17 00:00:48,180 --> 00:00:51,780 It takes in the person's name as a parameter, 18 00:00:51,780 --> 00:00:55,200 and the salary and a full-time status of boolean. 19 00:00:55,200 --> 00:00:57,450 And it returns an employee, 20 00:00:57,450 --> 00:01:00,360 it returns an employee object by value. 21 00:01:00,360 --> 00:01:01,620 And that's what this does. 22 00:01:01,620 --> 00:01:03,990 Remember when you have a function, 23 00:01:03,990 --> 00:01:06,390 if you have an expression at the end of the function, 24 00:01:06,390 --> 00:01:08,550 then it's like having a return statement. 25 00:01:08,550 --> 00:01:11,015 This is returning an employee 26 00:01:11,015 --> 00:01:15,270 whose name field is set to the name parameter 27 00:01:15,270 --> 00:01:19,080 and whose salary field is set to the salary parameter 28 00:01:19,080 --> 00:01:23,040 and whose full-time status is set to full-time status. 29 00:01:23,040 --> 00:01:26,130 Okay, so you pass three parameters into this function. 30 00:01:26,130 --> 00:01:29,580 What you'll get back is a properly initialized employee. 31 00:01:29,580 --> 00:01:32,010 These are the names of the fields 32 00:01:32,010 --> 00:01:33,480 and these are the parameters 33 00:01:33,480 --> 00:01:36,378 that we're going to use to initialize them. 34 00:01:36,378 --> 00:01:39,060 So this is a builder function. 35 00:01:39,060 --> 00:01:42,210 It encapsulates creating a structure instance. 36 00:01:42,210 --> 00:01:44,340 It returns the structure back to the caller. 37 00:01:44,340 --> 00:01:47,478 Here's the employee that's just been built. 38 00:01:47,478 --> 00:01:49,290 Okay, so it's quite common 39 00:01:49,290 --> 00:01:51,923 to like build a functions like this. 40 00:01:51,923 --> 00:01:55,530 As an aside, the syntax that we've got here 41 00:01:55,530 --> 00:01:59,760 where we kind of manually initialize the name field 42 00:01:59,760 --> 00:02:03,240 to be the value of the name parameter and the salary field 43 00:02:03,240 --> 00:02:06,090 to be the salary and the full-time field 44 00:02:06,090 --> 00:02:08,040 to be the full-time status like that, 45 00:02:08,040 --> 00:02:11,673 this syntax can be simplified to this syntax, 46 00:02:12,870 --> 00:02:17,040 if the field name is the same as the value name, 47 00:02:17,040 --> 00:02:20,070 if you like, if the field is called salary, 48 00:02:20,070 --> 00:02:22,470 and the parameter is called salary, 49 00:02:22,470 --> 00:02:25,290 then instead of saying name equals name, 50 00:02:25,290 --> 00:02:28,290 salary equals salary, full-time equals full-time, 51 00:02:28,290 --> 00:02:31,230 you can just use this syntax, this abbreviated syntax is 52 00:02:31,230 --> 00:02:33,120 what you'd actually use in practice. 53 00:02:33,120 --> 00:02:37,110 It only works if the name of the field is the same 54 00:02:37,110 --> 00:02:38,310 as the parameter. 55 00:02:38,310 --> 00:02:42,810 Okay, it'll use the name parameter to initialize the name, 56 00:02:42,810 --> 00:02:45,690 the salary parameter to initialize the salary 57 00:02:45,690 --> 00:02:47,310 and the full-time parameter 58 00:02:47,310 --> 00:02:49,290 to initialize the full-time field. 59 00:02:49,290 --> 00:02:51,300 Okay, so obviously that makes sense, 60 00:02:51,300 --> 00:02:54,570 just make sure that when you name these parameters, 61 00:02:54,570 --> 00:02:56,460 they have the same name as the fields 62 00:02:56,460 --> 00:02:58,519 and they will automatically be transferred 63 00:02:58,519 --> 00:03:01,440 into each field respectively, so that syntax, 64 00:03:01,440 --> 00:03:05,220 you would agree, is a lot simpler than that syntax. 65 00:03:05,220 --> 00:03:07,080 So let's go for this. 66 00:03:07,080 --> 00:03:08,700 It means that when we call the function, 67 00:03:08,700 --> 00:03:10,050 we'll get back an employee. 68 00:03:10,980 --> 00:03:14,160 So let's have a look at how that works. 69 00:03:14,160 --> 00:03:18,210 We can mark the employee as mutable if we want to or not. 70 00:03:18,210 --> 00:03:19,440 If we don't, 71 00:03:19,440 --> 00:03:21,993 so I've called the builder function build_employee. 72 00:03:22,920 --> 00:03:24,180 I've passed in three parameters, 73 00:03:24,180 --> 00:03:26,520 a string for the person's name, their salary, 74 00:03:26,520 --> 00:03:27,870 and their full-time status. 75 00:03:27,870 --> 00:03:30,420 This will gimme back an employee object, 76 00:03:30,420 --> 00:03:33,270 which in this first case is not mutable. 77 00:03:33,270 --> 00:03:37,357 In the second case I call the build_employee function again, 78 00:03:37,357 --> 00:03:39,330 I pass in different parameters. 79 00:03:39,330 --> 00:03:41,490 I get back another employee object 80 00:03:41,490 --> 00:03:44,670 which I assign to e2 and e2 is mutable, 81 00:03:44,670 --> 00:03:47,700 which means I can change anything in the employee object. 82 00:03:47,700 --> 00:03:51,060 So having these builder functions is convenient 83 00:03:51,060 --> 00:03:55,410 because it kind of encapsulates the creation of the object 84 00:03:55,410 --> 00:03:59,400 and the passing in of fields into the object. 85 00:03:59,400 --> 00:04:01,560 It makes it a neater syntax. 86 00:04:01,560 --> 00:04:02,940 Right, so let's have a look. 87 00:04:02,940 --> 00:04:04,800 Usual project. 88 00:04:04,800 --> 00:04:05,850 The files we're going to look at, 89 00:04:05,850 --> 00:04:09,450 obviously mytypes.rs defines the employee structure. 90 00:04:09,450 --> 00:04:12,690 main.rs coordinates the whole program 91 00:04:12,690 --> 00:04:15,270 and the demo file for this part of the example, 92 00:04:15,270 --> 00:04:19,620 demo_struck_return_value and then we'll run it at the end. 93 00:04:19,620 --> 00:04:20,763 So let's have a look. 94 00:04:22,020 --> 00:04:25,470 Okay, so mytypes.rs, that's the employee structure. 95 00:04:25,470 --> 00:04:27,180 We've seen that many times now. 96 00:04:27,180 --> 00:04:28,500 My main code, 97 00:04:28,500 --> 00:04:30,950 I'm going to un-comment demo_struck_return_value, 98 00:04:32,340 --> 00:04:34,653 and let's have a look at it here. 99 00:04:35,940 --> 00:04:40,940 All right, so what have we got going on here then. 100 00:04:42,570 --> 00:04:45,870 I've got a builder function, which when you call it 101 00:04:45,870 --> 00:04:49,560 with a person's name and salary and full-time status, 102 00:04:49,560 --> 00:04:51,570 it returns an employee object. 103 00:04:51,570 --> 00:04:53,190 Let's have a look. 104 00:04:53,190 --> 00:04:57,000 Okay, so this one is using the kind of verbose syntax 105 00:04:57,000 --> 00:05:01,050 where it takes the parameter name and stores it in name, 106 00:05:01,050 --> 00:05:03,810 it takes the salary parameter and transfers it 107 00:05:03,810 --> 00:05:07,740 to the salary field, and the full-time flag 108 00:05:07,740 --> 00:05:10,440 and passes it in here, so in this case here, 109 00:05:10,440 --> 00:05:13,320 I could have called these parameters n, 110 00:05:13,320 --> 00:05:16,920 or anything really, an s, an f. 111 00:05:16,920 --> 00:05:19,080 Okay and then here I'd have said 112 00:05:19,080 --> 00:05:21,660 the name field is initialized with n, 113 00:05:21,660 --> 00:05:24,240 the salary field is initialized with s, 114 00:05:24,240 --> 00:05:27,390 and the full-time field is initialized with f. 115 00:05:27,390 --> 00:05:30,090 Okay, it's effectively doing that, 116 00:05:30,090 --> 00:05:31,800 but obviously it makes a lot more sense 117 00:05:31,800 --> 00:05:34,350 to give the parameters the same names as the fields. 118 00:05:34,350 --> 00:05:36,573 I've called this build_employee_v2, 119 00:05:37,650 --> 00:05:38,850 and it's much better. 120 00:05:38,850 --> 00:05:43,380 So there it basically creates an employee object 121 00:05:43,380 --> 00:05:47,400 and implicitly initializes the name with the name parameter, 122 00:05:47,400 --> 00:05:50,250 the salary with a salary parameter, 123 00:05:50,250 --> 00:05:52,440 and the full-time with a full-time parameter. 124 00:05:52,440 --> 00:05:56,520 And this only works because the fields have the same name 125 00:05:56,520 --> 00:05:57,540 as the parameters. 126 00:05:57,540 --> 00:06:02,540 If this parameter was called n and s and f, 127 00:06:03,330 --> 00:06:04,650 then this wouldn't work. 128 00:06:04,650 --> 00:06:06,319 It would say, I'm looking 129 00:06:06,319 --> 00:06:09,750 for like an external variable called name, where is it? 130 00:06:09,750 --> 00:06:12,360 You'd have to use name, colon, n, 131 00:06:12,360 --> 00:06:14,040 and then we're back to square one. 132 00:06:14,040 --> 00:06:15,870 So let's not do that. 133 00:06:15,870 --> 00:06:19,470 Let's name the prompters the same as the field names, 134 00:06:19,470 --> 00:06:22,260 and then they will automatically be transferred here. 135 00:06:22,260 --> 00:06:24,870 And like I was saying, when you have a function 136 00:06:24,870 --> 00:06:26,730 with an expression at the end, 137 00:06:26,730 --> 00:06:30,330 then it's implicitly the return value, so if I wanted to, 138 00:06:30,330 --> 00:06:34,440 I could have said return employee semicolon, 139 00:06:34,440 --> 00:06:36,210 but if it's the last expression, 140 00:06:36,210 --> 00:06:38,400 then it's implicitly the the return value. 141 00:06:38,400 --> 00:06:42,420 So that's the way Crusty the Crustacean would write it. 142 00:06:42,420 --> 00:06:44,430 Oh and then finally, I've got a function 143 00:06:44,430 --> 00:06:46,590 that takes an employee by reference, 144 00:06:46,590 --> 00:06:48,960 and it prints the data for the employee, 145 00:06:48,960 --> 00:06:52,470 the name and the salary and the full-time status. 146 00:06:52,470 --> 00:06:55,323 Okay, so let's just run the code up here. 147 00:06:56,310 --> 00:06:59,700 So I create an employee using the first version 148 00:06:59,700 --> 00:07:03,900 of the builder, and e1 isn't mutable. 149 00:07:03,900 --> 00:07:05,880 I print the employee details 150 00:07:05,880 --> 00:07:09,750 and then I create employee two using the second version 151 00:07:09,750 --> 00:07:11,130 of my builder function, 152 00:07:11,130 --> 00:07:15,240 the improved version with the more concise syntax down here. 153 00:07:15,240 --> 00:07:17,700 And that employee object would be John. 154 00:07:17,700 --> 00:07:20,310 And I've decided to make that employee mutable, 155 00:07:20,310 --> 00:07:23,523 which means I can employee salary rise, 156 00:07:24,537 --> 00:07:27,030 and then finally I print employee two as well. 157 00:07:27,030 --> 00:07:29,880 So I feel like we should just run this just to make sure. 158 00:07:35,950 --> 00:07:40,200 Okay, so I built employee one, Jane, 1000, true. 159 00:07:40,200 --> 00:07:44,610 And then I print employee one, Jane, 1000, true. 160 00:07:44,610 --> 00:07:47,310 And then I create a mutable employee, 161 00:07:47,310 --> 00:07:50,970 John, 1000, false, give them a thousand pay rise 162 00:07:50,970 --> 00:07:54,210 and then print John's details and John's details are here. 163 00:07:54,210 --> 00:07:56,880 So this is a really widely used mechanism. 164 00:07:56,880 --> 00:07:59,850 We'll take it a step further in the next lesson 165 00:07:59,850 --> 00:08:01,920 and I'll show you what that means later. 166 00:08:01,920 --> 00:08:05,580 But having a function whose responsibility is 167 00:08:05,580 --> 00:08:08,580 to create and return an object 168 00:08:08,580 --> 00:08:10,920 with the initial values is a very useful thing 169 00:08:10,920 --> 00:08:11,880 because it means your code 170 00:08:11,880 --> 00:08:14,130 up here is just a simple function call. 171 00:08:14,130 --> 00:08:15,540 And you know, rather than having 172 00:08:15,540 --> 00:08:17,310 to create the employee explicitly, 173 00:08:17,310 --> 00:08:19,680 you get the employee back already initialized 174 00:08:19,680 --> 00:08:21,120 through the field so it's quite elegant 175 00:08:21,120 --> 00:08:22,520 and that's the way to do it.