1 00:00:05,530 --> 00:00:08,830 In this video we'll learn about passing arrays to c++ functions. 2 00:00:09,960 --> 00:00:13,570 We can pass arrays into functions by providing the square brackets 3 00:00:13,570 --> 00:00:14,890 in the parameter information. 4 00:00:15,010 --> 00:00:18,211 So in this case, we can have a function named print array 5 00:00:18,289 --> 00:00:21,240 that expects an array of integers and returns nothing. 6 00:00:22,220 --> 00:00:25,280 The idea is that this function iterates through the array 7 00:00:25,480 --> 00:00:27,100 and displays the array values. 8 00:00:27,420 --> 00:00:28,829 Pretty handy function, right. 9 00:00:29,280 --> 00:00:30,630 But arrays are different in c++. 10 00:00:31,599 --> 00:00:35,819 Remember that an array name evaluates to the address or location and memory 11 00:00:35,969 --> 00:00:37,610 of the first element of the array. 12 00:00:38,129 --> 00:00:39,860 In other words, the beginning of the array. 13 00:00:40,780 --> 00:00:43,720 So what's being passed into the function is not a copy of the 14 00:00:43,720 --> 00:00:47,309 entire array, but only the address of the first element of the array. 15 00:00:47,800 --> 00:00:51,410 That means that the function has no idea how many elements are in the array. 16 00:00:51,410 --> 00:00:54,110 So the programmer doesn't know how many times to iterate. 17 00:00:54,619 --> 00:00:58,310 So when we pass arrays to functions, we also need to pass in the size 18 00:00:58,310 --> 00:01:01,900 of the array, so that we now know how many times to iterate. 19 00:01:01,900 --> 00:01:02,960 Let me show you an example. 20 00:01:06,340 --> 00:01:09,210 In this example, you can see that both the function prototype 21 00:01:09,610 --> 00:01:12,240 and the function definition have square brackets after 22 00:01:12,240 --> 00:01:13,369 the formal parameter name. 23 00:01:14,920 --> 00:01:17,370 This tells the compiler that the function expects an array 24 00:01:17,370 --> 00:01:18,630 of integers in this case. 25 00:01:18,860 --> 00:01:20,030 So far so good. 26 00:01:20,210 --> 00:01:23,109 Then in main, I have an array of five integers called my 27 00:01:23,110 --> 00:01:26,359 numbers, and it's been initialized to the integers 1 through 5. 28 00:01:26,980 --> 00:01:30,960 So I call the print array function and pass in the array my numbers. 29 00:01:31,340 --> 00:01:32,530 All good so far. 30 00:01:32,780 --> 00:01:34,780 Now I execute the print array function. 31 00:01:35,270 --> 00:01:37,650 But I have no idea how many times I need to iterate. 32 00:01:37,900 --> 00:01:41,259 Since the array name has no size information, I'm stuck. 33 00:01:41,950 --> 00:01:45,409 If the array has a sentinel value, like a c-style string, 34 00:01:45,429 --> 00:01:47,980 then I can iterate until I see that sentinel value. 35 00:01:47,980 --> 00:01:51,790 But in this case, the my numbers array has no such sentinel value. 36 00:01:52,380 --> 00:01:55,050 There's no way I can write this function body in a way that it will 37 00:01:55,070 --> 00:01:56,730 work with any array of integers. 38 00:01:57,309 --> 00:02:01,130 So the solution is to pass in the size of the array to the function as well. 39 00:02:01,460 --> 00:02:02,790 Let's do that next. 40 00:02:05,609 --> 00:02:08,699 Now we've added a size parameter to both the function prototype 41 00:02:08,758 --> 00:02:10,139 and the function definition. 42 00:02:11,150 --> 00:02:15,069 In the main, when I call the function, I pass in my numbers and the size of 43 00:02:15,070 --> 00:02:16,850 the array which is 5 in this case. 44 00:02:17,719 --> 00:02:20,949 Now when i execute the body of the print array function, I know 45 00:02:20,950 --> 00:02:24,020 the location of the array which is the array name and I know how 46 00:02:24,020 --> 00:02:25,360 many elements are in the array. 47 00:02:25,680 --> 00:02:28,299 I can easily write a for loop now that iterates through the 48 00:02:28,300 --> 00:02:29,810 array and displays every integer. 49 00:02:30,570 --> 00:02:33,099 This looks great but there is one potential gotcha that 50 00:02:33,099 --> 00:02:34,180 you need to be aware of. 51 00:02:36,820 --> 00:02:40,229 Since we're passing into the function, the location of the actual array 52 00:02:40,230 --> 00:02:43,820 that was declared in main that means that we can modify that actual 53 00:02:43,820 --> 00:02:44,990 array from within the function. 54 00:02:45,630 --> 00:02:48,929 This could be useful in the case of a function like zero array that 55 00:02:48,930 --> 00:02:51,940 we can call whenever we want to 0 out all the elements in an array. 56 00:02:52,490 --> 00:02:55,730 However, in the print array function, we don't want to modify the array. 57 00:02:56,059 --> 00:02:58,419 If we do it's probably an unintentional error. 58 00:02:59,320 --> 00:03:01,769 We'll see how to protect ourselves from this kind of error in the 59 00:03:01,770 --> 00:03:04,780 next slide, but first let's see how we could write a useful 60 00:03:04,780 --> 00:03:06,460 function like zero array. 61 00:03:06,720 --> 00:03:10,500 In this case the zero array function receives the location of the array 62 00:03:10,500 --> 00:03:12,060 and the size of the array. 63 00:03:12,660 --> 00:03:14,940 We could simply iterate through the array and set 64 00:03:14,940 --> 00:03:16,419 each array element to zero. 65 00:03:17,279 --> 00:03:20,620 Notice that when we print the array in main after we call zero array, 66 00:03:20,640 --> 00:03:22,330 all the array elements are now zero. 67 00:03:23,090 --> 00:03:25,410 So let's get back to see what we can do to have the compiler 68 00:03:25,420 --> 00:03:28,389 help us, so we don't change an array we don't want to. 69 00:03:29,169 --> 00:03:32,530 We can define function parameters as const parameters. 70 00:03:32,830 --> 00:03:36,530 This tells the compiler that these parameters are read-only within the function body. 71 00:03:37,230 --> 00:03:40,110 Any attempt to modify them will result in a compiler error. 72 00:03:40,760 --> 00:03:42,710 So in this case notice that we included the const 73 00:03:43,260 --> 00:03:46,040 keyword right before the array parameter declaration. 74 00:03:46,640 --> 00:03:47,520 That's it. 75 00:03:47,560 --> 00:03:50,490 Now if I try to modify any array element as i do in 76 00:03:50,490 --> 00:03:53,239 the assignment statement, a compiler error will occur. 77 00:03:53,759 --> 00:03:56,920 Depending on your compiler, the error message will say something like error 78 00:03:56,920 --> 00:03:58,910 trying to assign to a read-only value. 79 00:03:59,910 --> 00:04:02,900 The idea of passing the location of a variable to a function 80 00:04:03,139 --> 00:04:06,669 instead of the value of a variable is fundamental in understanding 81 00:04:06,670 --> 00:04:09,929 the next topic we'll talk about, it's called pass by reference. 82 00:04:11,449 --> 00:04:14,000 Before we talk about pass by reference, I want to be sure that 83 00:04:14,000 --> 00:04:17,529 you really understand the whole idea of passing arrays to functions. 84 00:04:17,899 --> 00:04:21,226 So I'm in the IDE and I'm in the section 11 workspace, the 85 00:04:21,309 --> 00:04:23,580 arrays and functions project. 86 00:04:23,900 --> 00:04:25,450 And let me show you what I've got here. 87 00:04:25,570 --> 00:04:26,800 Let's start at the main. 88 00:04:27,139 --> 00:04:30,690 I've got a real simple array here, array of integers called my scores, 89 00:04:30,700 --> 00:04:35,549 and I've initialized it to 100 98 90 86 and 84, right 5 integers. 90 00:04:36,430 --> 00:04:38,929 Now what I want to do is i want to call a function called print 91 00:04:38,940 --> 00:04:43,269 array, and i want to pass into that function the array and the size 92 00:04:43,270 --> 00:04:44,700 of the array, as we saw before. 93 00:04:45,400 --> 00:04:45,980 Perfect. 94 00:04:46,070 --> 00:04:49,600 That should print out 100 98 90 86 and 84. 95 00:04:50,610 --> 00:04:52,630 Now I've got a function called set array. 96 00:04:52,940 --> 00:04:55,480 And what this function will do is it'll set every 97 00:04:55,480 --> 00:04:57,640 value of that array to 100. 98 00:04:58,420 --> 00:05:01,080 So when i come back, it'll print out 100 five times, so that 99 00:05:01,080 --> 00:05:02,820 will actually modify the array. 100 00:05:03,330 --> 00:05:04,830 So let's see what these functions look like. 101 00:05:04,830 --> 00:05:06,180 They're pretty straightforward. 102 00:05:06,230 --> 00:05:07,980 There are the function prototypes. 103 00:05:08,110 --> 00:05:09,940 That's the function prototype for print array. 104 00:05:09,940 --> 00:05:13,930 It expects an array of integers and the size of the array. 105 00:05:14,340 --> 00:05:18,090 The set array function expects an array of integers, the size of the 106 00:05:18,090 --> 00:05:21,119 array and the value that we want to set each one of those elements to. 107 00:05:21,880 --> 00:05:25,569 All right, so let's look at the print array and I put const 108 00:05:25,790 --> 00:05:28,560 here because it's something that we need to think about. 109 00:05:28,560 --> 00:05:31,060 Whenever we write functions that work with arrays. 110 00:05:32,099 --> 00:05:33,190 So let's look at this. 111 00:05:33,389 --> 00:05:34,139 What do we do here? 112 00:05:34,139 --> 00:05:35,310 Well, we iterate. 113 00:05:35,349 --> 00:05:37,349 We iterate for I equal to 0. 114 00:05:37,349 --> 00:05:41,079 While I is less than size, that's the the idiom that we do all 115 00:05:41,080 --> 00:05:44,580 the time when we're working with arrays, and we're incrementing by 1. 116 00:05:44,600 --> 00:05:47,420 And then all I'm doing is I'm outputting array sub-I. 117 00:05:47,740 --> 00:05:51,370 So I'm going to go from array sub-0, array sub-1, array sub-2, all the 118 00:05:51,370 --> 00:05:53,190 way to array sub-4, in this case. 119 00:05:53,509 --> 00:05:55,520 And I'm going to print out each one of those integers. 120 00:05:55,520 --> 00:05:57,420 Then I'm just going to print a new line at the end. 121 00:05:58,000 --> 00:06:00,810 That will print out 100 98 90 86 and 84. 122 00:06:02,260 --> 00:06:04,530 Perfect. So that's that line right here. 123 00:06:05,030 --> 00:06:07,120 Then I'm going to call set array, and I'm going to pass 124 00:06:07,120 --> 00:06:10,990 in, again, the array, the 5, which is the size and 100. 125 00:06:11,420 --> 00:06:15,960 In this case, rather than doing any outputting, what I want to do is 126 00:06:16,129 --> 00:06:19,210 I want to loop through that array again, so the same loop from I 127 00:06:19,210 --> 00:06:20,844 equals 0, I less than size by one. 128 00:06:20,844 --> 00:06:24,729 What I want to do at each iteration of the loop is I 129 00:06:24,729 --> 00:06:27,469 want to set arr sub-I, right. 130 00:06:27,489 --> 00:06:31,090 The Ith element of that array to the value that was passed in. 131 00:06:31,599 --> 00:06:34,340 So in this case, I'm modifying all five of the array integers to 132 00:06:34,340 --> 00:06:36,940 100, then I'm printing it again. 133 00:06:37,330 --> 00:06:40,820 So what I expect from this program is the first print array statement is 134 00:06:40,860 --> 00:06:45,020 going to print 100 98 90 86 and 84. 135 00:06:45,020 --> 00:06:49,179 Then the last print array statement here is going to print five 100s. 136 00:06:49,990 --> 00:06:53,249 Okay, so let's run this, and then we'll walk through this in detail so 137 00:06:53,270 --> 00:06:54,739 you can see exactly what's going on. 138 00:06:57,239 --> 00:07:03,229 There you go 100 98 90 86 84, and then 500, exactly what we expected. 139 00:07:03,909 --> 00:07:05,239 So let's walk through this. 140 00:07:05,239 --> 00:07:08,010 Let's start with the print array function first. 141 00:07:08,619 --> 00:07:12,330 And my main has that array right here called my scores. 142 00:07:12,330 --> 00:07:14,070 And I'm going to draw this visually, so you can see 143 00:07:14,070 --> 00:07:15,370 exactly what's going on. 144 00:07:15,800 --> 00:07:16,859 There's my scores. 145 00:07:17,170 --> 00:07:19,390 It's an array of five integers. 146 00:07:21,429 --> 00:07:29,179 Okay, and I'll put them in here: 100 98 90 86 and 84. 147 00:07:29,309 --> 00:07:32,950 The value of the name of an array is the location of that 148 00:07:32,950 --> 00:07:34,480 first element in the array. 149 00:07:34,560 --> 00:07:38,320 So let's say that that integer 100 here is at location 2000. 150 00:07:39,020 --> 00:07:41,399 And I'm just making that up, it could be any location. 151 00:07:42,329 --> 00:07:43,420 That's 2000. 152 00:07:43,679 --> 00:07:48,509 So whenever the c++ compiler sees an array name, this 153 00:07:48,530 --> 00:07:49,530 is what it's going at. 154 00:07:49,530 --> 00:07:52,390 In this case, my scores will evaluate to 2000. 155 00:07:53,000 --> 00:07:53,720 Okay, perfect. 156 00:07:54,170 --> 00:07:58,409 So now I'm going to call print array right here, this is what's going on. 157 00:07:59,120 --> 00:08:01,500 So now I'm in my print array function. 158 00:08:03,030 --> 00:08:06,460 And print array has several local variables. 159 00:08:06,460 --> 00:08:11,230 It's got array, it's got size, and it's got this I right there. 160 00:08:11,230 --> 00:08:14,160 Let's just worry about the array and the size variables right now. 161 00:08:14,860 --> 00:08:18,672 So there's my arr, I'll just call it array. 162 00:08:18,672 --> 00:08:19,890 And here's my size. 163 00:08:21,529 --> 00:08:24,510 When the function is called, the values are copied. 164 00:08:25,000 --> 00:08:30,320 So my scores is 2000, it's not the entire array, it's 2000. 165 00:08:30,320 --> 00:08:32,100 So 2000 gets copied over here. 166 00:08:33,400 --> 00:08:34,320 Size is the 5. 167 00:08:34,808 --> 00:08:37,539 So size gets the 5, right. 168 00:08:38,110 --> 00:08:39,409 Now I start looping. 169 00:08:39,840 --> 00:08:43,789 And the first thing I do is it says I'm -- basically, I is zero right. 170 00:08:43,950 --> 00:08:50,450 So what I'm going to do is I'm going to cout arr sub-0. 171 00:08:51,389 --> 00:08:52,440 Well, what does that mean? 172 00:08:52,670 --> 00:08:55,249 That means the first integer in the array. 173 00:08:55,259 --> 00:08:57,460 Well, where's the array, right here. 174 00:08:58,340 --> 00:08:59,699 That's that guy, right there. 175 00:08:59,809 --> 00:09:03,220 So you can see that this print array function is referring to 176 00:09:03,220 --> 00:09:07,270 the actual array in main because the array was never copied. 177 00:09:07,270 --> 00:09:10,140 Instead, the address of the array was passed in. 178 00:09:10,990 --> 00:09:13,149 It's going to go through each one of these through the loop. 179 00:09:13,179 --> 00:09:17,360 It's going to print out arr sub-1, which is that guy right there 98. 180 00:09:17,650 --> 00:09:19,130 So it's going to display them all. 181 00:09:20,489 --> 00:09:21,599 Okay, that makes sense. 182 00:09:21,599 --> 00:09:22,313 I hope so. 183 00:09:22,313 --> 00:09:27,330 So so again, this will print out right here, it'll display 100 98, 184 00:09:27,330 --> 00:09:30,980 just like we saw, 90 86 and 84. 185 00:09:31,660 --> 00:09:33,770 Now let's look at this function call right here. 186 00:09:34,050 --> 00:09:40,260 I still have my my scores array and it's still the same. 187 00:09:40,260 --> 00:09:41,260 I haven't changed it. 188 00:09:42,760 --> 00:09:44,379 And let me put those numbers back in here. 189 00:09:44,389 --> 00:09:50,750 I'll put 100 in here 98 90 86 and 84, right. 190 00:09:51,040 --> 00:09:52,170 Now I call set array. 191 00:09:52,990 --> 00:09:59,820 Well, set array is over here, and it's got same idea, right. 192 00:09:59,820 --> 00:10:02,450 It's got the arr variable, the size and the value. 193 00:10:02,530 --> 00:10:07,020 So it's got arr, it's got size and it's got value. 194 00:10:08,370 --> 00:10:11,319 Let's again assume that this is address 2000 right 195 00:10:11,320 --> 00:10:12,429 here that's that name. 196 00:10:13,280 --> 00:10:16,810 So in this case, I'm passing in the 2000, I'm passing in 5 for 197 00:10:16,810 --> 00:10:21,470 size and I'm passing in the 100 for the value, and then I'm looping. 198 00:10:21,820 --> 00:10:22,760 And notice that loop. 199 00:10:22,760 --> 00:10:24,810 Every time in the loop, this is what I'm doing right here. 200 00:10:24,960 --> 00:10:30,474 So the first time through I'm saying arr sub-0 is value which is 100. 201 00:10:30,699 --> 00:10:31,800 It's always going to be 100. 202 00:10:32,680 --> 00:10:36,330 Well, where is arr sub-0, it's at 2000. 203 00:10:36,610 --> 00:10:39,920 So what i just did was I just came in here and put 100 in here. 204 00:10:40,559 --> 00:10:45,170 Then I'm going to increment I and it becomes arr sub-1 equals 100. 205 00:10:45,799 --> 00:10:47,149 So this guy is over here. 206 00:10:47,150 --> 00:10:48,449 I just got rid of that one. 207 00:10:49,100 --> 00:10:52,470 And what I'm doing is I'm initializing the entire array 208 00:10:52,530 --> 00:10:57,699 to whatever value was passed in, which is 100 in this case, right. 209 00:10:57,980 --> 00:11:01,709 When this function is finished, it's gone. 210 00:11:02,549 --> 00:11:04,879 And what I'm left with is the updated array. 211 00:11:04,969 --> 00:11:08,810 So this will print out 5 100s as you saw. 212 00:11:10,260 --> 00:11:11,670 Okay, so hopefully, that makes sense. 213 00:11:11,680 --> 00:11:13,830 It's a real different from pass by value. 214 00:11:14,280 --> 00:11:17,779 But when you think about it, it is still doing pass by value because 215 00:11:17,780 --> 00:11:20,599 it's copying the value of 2000, right. 216 00:11:21,059 --> 00:11:23,830 It's not copying the entire array but it's copying 2000, 217 00:11:24,570 --> 00:11:28,240 which is what my score is defaults to, that's its value. 218 00:11:28,970 --> 00:11:31,000 All right, so one last thing. 219 00:11:31,960 --> 00:11:37,190 We really don't want to be able to change that array in the print array. 220 00:11:37,929 --> 00:11:41,629 Let's say that we did something like this something really silly 221 00:11:41,629 --> 00:11:45,490 we could say arr sub-0 is 10,000. 222 00:11:46,270 --> 00:11:47,810 Well, you know what let's see 50,000. 223 00:11:47,820 --> 00:11:50,020 Just so that we could really see the difference. 224 00:11:50,400 --> 00:11:52,630 Okay, this is probably a bug. 225 00:11:54,150 --> 00:11:56,640 Okay, print functions don't tend to change anything. 226 00:11:56,640 --> 00:11:58,130 They just display information. 227 00:11:58,350 --> 00:12:00,740 But let's just say that we've got some bug in our code. 228 00:12:01,440 --> 00:12:05,200 Now right here, I'll call print array again. 229 00:12:08,210 --> 00:12:11,300 And I'll call it exactly the same with my scores and 5. 230 00:12:12,939 --> 00:12:14,089 So look what happened here. 231 00:12:14,670 --> 00:12:16,110 I called print array here. 232 00:12:16,180 --> 00:12:17,880 It printed out the 5 100s. 233 00:12:18,270 --> 00:12:20,555 But then it changed one of them, the first one to 50,000. 234 00:12:20,880 --> 00:12:23,260 And then I'm calling it again. 235 00:12:23,800 --> 00:12:27,410 So I'm expecting to see that first, there it is. 236 00:12:27,420 --> 00:12:27,890 You see it. 237 00:12:27,900 --> 00:12:32,165 That first array element right there just got changed to 50,000. 238 00:12:33,330 --> 00:12:34,449 How do i prevent that. 239 00:12:34,850 --> 00:12:38,220 Real easy way to prevent that is to come up to your print array 240 00:12:38,220 --> 00:12:43,260 math function right here, and say that this is a constant array. 241 00:12:43,790 --> 00:12:47,170 And I can do the same thing in the definition. 242 00:12:48,380 --> 00:12:52,419 Now if you try to compile this, there's the error assignment 243 00:12:52,420 --> 00:12:55,969 of read-only location arr. 244 00:12:56,130 --> 00:13:00,340 So in other words, this function now print array cannot change array. 245 00:13:00,640 --> 00:13:01,960 That's really what we want. 246 00:13:02,050 --> 00:13:03,579 So we can comment that out. 247 00:13:04,190 --> 00:13:05,460 And now we'll run just fine. 248 00:13:06,889 --> 00:13:09,089 Oka, notice that's exactly what we want. 249 00:13:09,360 --> 00:13:13,860 So when you're writing functions and they're passing around objects, 250 00:13:13,860 --> 00:13:18,150 especially arrays and vectors and things like that, especially when 251 00:13:18,150 --> 00:13:21,569 we talk about pass by reference in the next section, you've got to be 252 00:13:21,570 --> 00:13:24,350 really aware of what can change what. 253 00:13:24,420 --> 00:13:27,670 And if you do have access to the actual and you don't want to 254 00:13:27,670 --> 00:13:29,810 change it, const is the way to go. 255 00:13:30,299 --> 00:13:33,010 Okay, we'll talk more about that when we talk about pass by reference.