1 00:00:00,560 --> 00:00:01,910 When we write a function, 2 00:00:01,910 --> 00:00:06,400 we add code inside the body of the function within braces, right? 3 00:00:06,400 --> 00:00:09,500 Have you tried accessing that code from outside the function? 4 00:00:09,500 --> 00:00:10,050 Let's give it a go. 5 00:00:11,080 --> 00:00:14,774 >> Okay, let's tackle our last topic for this course, 6 00:00:14,774 --> 00:00:20,040 we'll add a new comment, and we'll say Scope, if I can type, there we go. 7 00:00:20,040 --> 00:00:23,757 Let's write a simple function that doesn't really do anything, but 8 00:00:23,757 --> 00:00:26,850 will serve to illustrate this concept of scope. 9 00:00:26,850 --> 00:00:30,265 We'll call this function array modifier. 10 00:00:30,265 --> 00:00:37,790 It has a single parameter named array, and we'll give the type array of int. 11 00:00:39,190 --> 00:00:41,180 Inside the body of the function, 12 00:00:41,180 --> 00:00:44,930 let's create a variable Let's name this arrayOfInts. 13 00:00:46,800 --> 00:00:48,580 Very creative, I know. 14 00:00:48,580 --> 00:00:53,470 And to this, we'll assign the array that we pass in as an argument. 15 00:00:53,470 --> 00:00:58,090 Okay, next, let's append an integer to this array of integers. 16 00:00:58,090 --> 00:01:00,450 So, we'll say arrayOfInts.append. 17 00:01:00,450 --> 00:01:04,990 You should remember how to append things from our previous lessons. 18 00:01:04,990 --> 00:01:09,950 So .append, by the way, now you know that append is a function, right? 19 00:01:09,950 --> 00:01:14,760 It takes an element which will pass in as an argument, and 20 00:01:14,760 --> 00:01:17,780 now this array has five added to it. 21 00:01:17,780 --> 00:01:22,242 Finally, for the last bit of code, in the body of the loop, 22 00:01:22,242 --> 00:01:27,177 let's assign arrayofInts to a new variable named secondArray. 23 00:01:27,177 --> 00:01:32,280 So, let's say, var secondArray = arrayOfInts, and that's it for 24 00:01:32,280 --> 00:01:37,230 the body of the function, like I said, it doesn't do much. 25 00:01:37,230 --> 00:01:41,500 So, here we have a function that takes an array as an argument, 26 00:01:41,500 --> 00:01:45,660 assigns it to a new variable, that we create within the function, 27 00:01:45,660 --> 00:01:50,620 modifies that array, and then assigns it again to yet a new variable. 28 00:01:50,620 --> 00:01:53,040 Now, outside the body of the function, 29 00:01:53,040 --> 00:01:57,390 let's create another new array that we will use with our function. 30 00:01:57,390 --> 00:02:01,510 So, I'm going to name this array arrayOfInts as well. 31 00:02:01,510 --> 00:02:03,110 So, var arrayOfInts. 32 00:02:04,810 --> 00:02:08,690 Now, if you're asking why can't I give it a different name to avoid confusion? 33 00:02:08,690 --> 00:02:09,660 Well, that's intentional. 34 00:02:10,670 --> 00:02:15,593 So, to this we'll add a few values, we'll say 1, 2, 3 and 4, and 35 00:02:15,593 --> 00:02:19,700 now let's call the function arrayModifier. 36 00:02:19,700 --> 00:02:25,150 And pass in this arrayOfInts as an argument to the function. 37 00:02:26,290 --> 00:02:30,290 So, based on what you know so far, let's see, if we can figure out what's going on. 38 00:02:30,290 --> 00:02:35,240 We declare an arrayOfInts and then we pass it into our function. 39 00:02:35,240 --> 00:02:38,790 Inside the function, we've assign at this array 40 00:02:38,790 --> 00:02:42,990 to another variable named arrayOfInts, and then we modify it. 41 00:02:42,990 --> 00:02:47,270 Now, it's hard for me to make you anticipate my answers to questions, 42 00:02:47,270 --> 00:02:51,880 because a playground just ruins it all for me by displaying the results already. 43 00:02:51,880 --> 00:02:58,368 As you can see here, the value of array of ints outside the function is 1, 2, 3, 4, 44 00:02:58,368 --> 00:03:04,638 but arrayOfInts inside the function is 1, 2, 3, 4, 5, which is different. 45 00:03:04,638 --> 00:03:08,894 So, inside the function, we're taking the array passed in as an argument, 46 00:03:08,894 --> 00:03:12,640 modifying it to add five, and then, obviously we have the result. 47 00:03:13,890 --> 00:03:18,950 So, what if we look at arrayOfInts after the function call? 48 00:03:18,950 --> 00:03:23,885 Well, you can see that it hasn't changed, so even though we're appending five, 49 00:03:23,885 --> 00:03:25,801 it still says 1, 2, 3, 4. 50 00:03:25,801 --> 00:03:30,153 So, the first thing you should know is that when you pass something 51 00:03:30,153 --> 00:03:33,804 into a function via a parameter, as we're doing here, 52 00:03:33,804 --> 00:03:37,120 you don't pass in this exact same value. 53 00:03:37,120 --> 00:03:42,590 Instead, Swift makes a copy of that by default and assigns it to a new constant. 54 00:03:42,590 --> 00:03:46,700 So, this constant array doesn't contain this array exactly, 55 00:03:46,700 --> 00:03:48,430 it just makes a clone of that. 56 00:03:48,430 --> 00:03:54,260 So, we have another array that's 1, 2, 3, 4 inside, and assigns it to array. 57 00:03:54,260 --> 00:03:58,705 Now, inside the body of the function, we can't change the array passed in, 58 00:03:58,705 --> 00:04:03,960 well, because this array, the argument, is a constant. 59 00:04:03,960 --> 00:04:08,820 So, for our next step, we created a new variable, arrayOfInts. 60 00:04:08,820 --> 00:04:11,170 With the same name as the one outside of it, 61 00:04:11,170 --> 00:04:14,590 then assigned an array containing those same values yet again. 62 00:04:14,590 --> 00:04:19,300 Now, here we successfully modified the array, because it's a variable. 63 00:04:19,300 --> 00:04:22,120 But it doesn't affect the one outside. 64 00:04:22,120 --> 00:04:25,430 And we know now that's why, because we are using a copy 65 00:04:25,430 --> 00:04:29,925 of the values in the outside array, not that array itself. 66 00:04:29,925 --> 00:04:33,780 We'll talk more about when and why this happens in a future lesson. 67 00:04:33,780 --> 00:04:39,440 Finally, we assign the modified array to a new constant second array. 68 00:04:39,440 --> 00:04:42,680 So, let's check what the final value of second array is 69 00:04:42,680 --> 00:04:45,170 after we make the function call outside of the function. 70 00:04:46,230 --> 00:04:48,890 So, if you hit enter and start typing second, 71 00:04:48,890 --> 00:04:54,140 you'll see that there is no second array, and Xcode auto complete doesn't show. 72 00:04:54,140 --> 00:04:55,105 What gives? 73 00:04:55,105 --> 00:04:55,810 Well, the reason for 74 00:04:55,810 --> 00:05:00,970 this is that second array doesn't actually exist outside of the function. 75 00:05:00,970 --> 00:05:05,370 This is because anything created within a function, that is anything created 76 00:05:05,370 --> 00:05:10,960 within these curly braces, exists only inside the function. 77 00:05:10,960 --> 00:05:14,930 And this is what is generally referred to as scope. 78 00:05:14,930 --> 00:05:18,590 A variable scope defines where it exists. 79 00:05:18,590 --> 00:05:19,720 And in this case, 80 00:05:19,720 --> 00:05:24,980 a variable created within the body of the function only exists inside. 81 00:05:24,980 --> 00:05:27,350 Now, there are some exceptions to these rules. 82 00:05:27,350 --> 00:05:32,300 But again, topic for the future, now, this concept of scope is also why 83 00:05:32,300 --> 00:05:37,235 we're able to create two variables of the same name, one inside the function, 84 00:05:37,235 --> 00:05:39,760 arrayofInts, and another outside the function. 85 00:05:40,770 --> 00:05:44,890 If you try to do this, for example, if we did var test = 1, 86 00:05:44,890 --> 00:05:48,870 and then did var test = 2, you should get an error, 87 00:05:48,870 --> 00:05:54,510 because we can't create two things with the same name in the same scope. 88 00:05:54,510 --> 00:05:56,060 You'll get an error. 89 00:05:56,060 --> 00:05:59,790 Within the same scope, names have to be unique. 90 00:05:59,790 --> 00:06:03,910 But in different scopes, you can have things with the same name. 91 00:06:03,910 --> 00:06:07,260 And so, over here inside the scope of the function, 92 00:06:07,260 --> 00:06:11,390 what is created inside the function, exists inside that function only. 93 00:06:11,390 --> 00:06:14,760 And so, we can get away with naming with the same name. 94 00:06:14,760 --> 00:06:17,830 Now, we've been talking about variables and constants for a while. 95 00:06:17,830 --> 00:06:20,420 But we haven't differentiated between those created 96 00:06:20,420 --> 00:06:22,960 inside a function versus outside. 97 00:06:22,960 --> 00:06:27,940 Any variable or constant that is created inside a function and assigned a value 98 00:06:27,940 --> 00:06:33,410 is called a local variable, so all of these are local variables. 99 00:06:33,410 --> 00:06:36,470 Outside of function, we call them global variables, 100 00:06:36,470 --> 00:06:38,670 because they're available globally. 101 00:06:38,670 --> 00:06:43,990 This means that if we declare an array, for example, if we go up to the top, 102 00:06:43,990 --> 00:06:48,690 and we declare an array, we'll say, let testArray = 1 and 103 00:06:48,690 --> 00:06:53,330 assign it to a constant like so outside the function, we can actually use it 104 00:06:53,330 --> 00:06:58,620 inside the function as well, because this constant is available globally. 105 00:06:58,620 --> 00:07:01,080 We're not defining it within a set of braces, so 106 00:07:01,080 --> 00:07:03,620 it's available in the global scope. 107 00:07:03,620 --> 00:07:07,370 Now, in general though, because a function is a block of code 108 00:07:07,370 --> 00:07:12,160 that we want to treat as a stand alone unit, it's preferable 109 00:07:12,160 --> 00:07:17,140 that we avoid the use of global variables in constants as much as we can. 110 00:07:17,140 --> 00:07:20,200 If we need any values from outside of the global scope 111 00:07:20,200 --> 00:07:23,610 inside of the function to help facilitate any logic, 112 00:07:23,610 --> 00:07:28,110 then it's better to pass those values in as arguments to the function. 113 00:07:28,110 --> 00:07:31,560 This way, we know exactly where the data is coming from. 114 00:07:31,560 --> 00:07:33,160 So, just keep that in mind. 115 00:07:33,160 --> 00:07:36,400 Local variables treated inside the body of a function 116 00:07:36,400 --> 00:07:38,920 exist only inside that function. 117 00:07:38,920 --> 00:07:42,120 Global variables, on the other hand, are available everywhere. 118 00:07:42,120 --> 00:07:45,750 Now, we've talked about enough, so it's time to wrap up this course. 119 00:07:45,750 --> 00:07:48,840 But I want to introduce you to one minor topic that we'll 120 00:07:48,840 --> 00:07:50,920 talk about more in the future. 121 00:07:50,920 --> 00:07:55,950 So, I mentioned earlier that append was a function as well, but you'll notice that 122 00:07:55,950 --> 00:08:01,340 append does not have any arguments, or it doesn't have any argument labels. 123 00:08:01,340 --> 00:08:05,780 Whereas, every function that we've written, or we had an argument label, now, 124 00:08:05,780 --> 00:08:10,310 you can actually create an argument that does not have a label yourself. 125 00:08:10,310 --> 00:08:12,480 I'll give you a quick preview. 126 00:08:12,480 --> 00:08:14,534 So, I'll say function, someFunction. 127 00:08:16,845 --> 00:08:19,130 And then let's say we have some argument. 128 00:08:19,130 --> 00:08:20,690 We'll call it test. 129 00:08:20,690 --> 00:08:25,230 Just for the sake of this, and we'll say Int for the type. 130 00:08:25,230 --> 00:08:26,580 Now, when we call some function, 131 00:08:26,580 --> 00:08:29,310 you'll notice that it has an argument label name test. 132 00:08:30,690 --> 00:08:35,530 To not have an argument label outside, we do the same thing as we did earlier, 133 00:08:35,530 --> 00:08:39,200 in that we give it an external name and a local name. 134 00:08:39,200 --> 00:08:43,130 But for the external name, we simply specify an underscore. 135 00:08:43,130 --> 00:08:46,830 And by doing that, we're telling Swift, hey, we want this to have 136 00:08:46,830 --> 00:08:51,490 no external name, but we do want it to have a local name of test. 137 00:08:51,490 --> 00:08:53,420 So, now if you call some function, 138 00:08:53,420 --> 00:08:58,130 you'll see that it does not have an argument label for this argument. 139 00:08:58,130 --> 00:09:04,150 I can simply pass in a value, and that is how the append function makes things work. 140 00:09:05,960 --> 00:09:08,850 And that brings us to the end of this course. 141 00:09:08,850 --> 00:09:12,690 Functions are where a lot of the power in programming comes from. 142 00:09:12,690 --> 00:09:15,920 And as we learn more, we'll be able to build together lots of 143 00:09:15,920 --> 00:09:18,770 useful functions to carry out interesting little tasks. 144 00:09:19,950 --> 00:09:23,110 Before you take off, let's get some practice in with the quiz and 145 00:09:23,110 --> 00:09:24,240 a coach challenge. 146 00:09:24,240 --> 00:09:25,950 After that, the next course awaits.