WEBVTT 1 00:00:00.340 --> 00:00:01.890 line:15% Hi, guys, and welcome back. 2 00:00:01.890 --> 00:00:04.210 line:15% In this video, we're going to learn about arguments 3 00:00:04.210 --> 00:00:06.200 line:15% and parameters, which we can use 4 00:00:06.200 --> 00:00:09.620 to make our functions generic so that we can use them 5 00:00:09.620 --> 00:00:12.360 with multiple different pieces of data. 6 00:00:12.360 --> 00:00:14.461 Let's say you've got a car dictionary 7 00:00:14.461 --> 00:00:16.260 that can take some information about a car, 8 00:00:16.260 --> 00:00:18.680 such as the make, model, the mileage, 9 00:00:18.680 --> 00:00:20.810 and the total amount of fuel it has consumed 10 00:00:20.810 --> 00:00:22.870 over its lifetime. 11 00:00:22.870 --> 00:00:25.020 You can calculate the miles per gallon, 12 00:00:25.020 --> 00:00:27.610 which I know is quite a British thing to be calculating, 13 00:00:27.610 --> 00:00:29.860 as the mileage of the car divided 14 00:00:29.860 --> 00:00:31.900 by the total amount of fuel consumed. 15 00:00:31.900 --> 00:00:35.220 Let's imagine that this is a unit in gallons. 16 00:00:35.220 --> 00:00:37.070 You could also get the name of the car 17 00:00:37.070 --> 00:00:39.730 as the make and the model. 18 00:00:39.730 --> 00:00:44.730 So I'm gonna create a f string with the make and the model. 19 00:00:45.280 --> 00:00:47.920 Finally, we can print something, like this car 20 00:00:47.920 --> 00:00:51.440 does this number of miles per gallon. 21 00:00:51.440 --> 00:00:53.150 If we run this programme, you'll see 22 00:00:53.150 --> 00:00:54.740 that there's nothing new here. 23 00:00:54.740 --> 00:00:57.690 You get Ford Fiesta does 50.0 miles per gallon, 24 00:00:57.690 --> 00:00:59.640 and that's all well and good. 25 00:00:59.640 --> 00:01:02.280 You know, you can actually put this into a function 26 00:01:02.280 --> 00:01:05.640 if you wanted to call this specific code many times. 27 00:01:05.640 --> 00:01:09.490 So what I'm gonna do is I'm going to put a def statement 28 00:01:09.490 --> 00:01:12.770 such as calculate_mpg and the colon. 29 00:01:12.770 --> 00:01:15.310 And what this does, again, is it creates 30 00:01:15.310 --> 00:01:17.840 a new function called calculate_mpg. 31 00:01:19.310 --> 00:01:21.650 Then the two brackets at the end, the colon, 32 00:01:21.650 --> 00:01:25.410 and all the code inside the body has to be indented. 33 00:01:25.410 --> 00:01:29.520 So I'm pressing the Tab key when I select all of these lines 34 00:01:29.520 --> 00:01:31.010 and that's gonna indent them all. 35 00:01:31.010 --> 00:01:34.530 It's gonna add four spaces in front of each line. 36 00:01:34.530 --> 00:01:37.340 Now, that we've got that, we can call this function 37 00:01:38.970 --> 00:01:40.780 so that it will run. 38 00:01:40.780 --> 00:01:43.570 So by pressing run now, the same exact thing 39 00:01:43.570 --> 00:01:45.880 will show up because we haven't really changed anything. 40 00:01:45.880 --> 00:01:47.970 All we've done is we've put this into a function, 41 00:01:47.970 --> 00:01:50.330 and then we've called the function, which runs the code. 42 00:01:50.330 --> 00:01:53.050 So it's the same as before, but a little bit longer 43 00:01:53.050 --> 00:01:55.750 because we now have to create the function. 44 00:01:55.750 --> 00:01:58.050 If all you wanted to do was this, 45 00:01:58.050 --> 00:02:00.340 you don't need a function for it. 46 00:02:00.340 --> 00:02:03.430 So, we're going to make this function a little bit better. 47 00:02:03.430 --> 00:02:04.590 We're gonna change this function 48 00:02:04.590 --> 00:02:08.720 so that it doesn't always use this data. 49 00:02:08.720 --> 00:02:10.440 We're gonna change it so that it can use 50 00:02:10.440 --> 00:02:13.660 any arbitrary piece of data that we pass to it 51 00:02:13.660 --> 00:02:16.880 given that it has this structure. 52 00:02:16.880 --> 00:02:20.760 So, I'm going to take this car out of the function, 53 00:02:20.760 --> 00:02:23.400 and I'm going to put it on top here. 54 00:02:23.400 --> 00:02:24.380 Let's say that. 55 00:02:24.380 --> 00:02:27.663 line:15% I'm going to make sure to un-indent it so it's all good. 56 00:02:28.910 --> 00:02:32.670 Now, if we run this again, this will still work, 57 00:02:32.670 --> 00:02:35.833 and that is because when you go into the function, 58 00:02:36.790 --> 00:02:39.870 and you try to access the car variable, 59 00:02:39.870 --> 00:02:42.980 notice that it's not defined inside the function, 60 00:02:42.980 --> 00:02:45.440 so Python realises that maybe 61 00:02:45.440 --> 00:02:47.130 it's defined outside the function. 62 00:02:47.130 --> 00:02:50.680 So it goes looking to the next block, 63 00:02:50.680 --> 00:02:53.020 which is the main application. 64 00:02:53.020 --> 00:02:56.990 And it can find the variable there, so it uses that. 65 00:02:56.990 --> 00:02:58.100 So that's all well and good. 66 00:02:58.100 --> 00:02:59.520 This still works. 67 00:02:59.520 --> 00:03:01.370 But, once again, we are limited 68 00:03:01.370 --> 00:03:04.713 by always using the variable out here. 69 00:03:05.640 --> 00:03:08.810 So what we're gonna do is we're going to put something 70 00:03:08.810 --> 00:03:10.830 inside the brackets. 71 00:03:10.830 --> 00:03:13.300 We're gonna put is car_to_calculate. 72 00:03:14.550 --> 00:03:17.240 And then inside the function, we're going to use 73 00:03:17.240 --> 00:03:19.723 this variable name instead of car. 74 00:03:24.210 --> 00:03:26.180 So I'm just going to expand this a little bit 75 00:03:26.180 --> 00:03:28.300 so it's a bit easier to read. 76 00:03:28.300 --> 00:03:30.850 What we've got now is that inside the function 77 00:03:30.850 --> 00:03:34.200 we're using this thing called car_to_calculate. 78 00:03:34.200 --> 00:03:36.070 And that is inside the brackets. 79 00:03:36.070 --> 00:03:37.373 So what does that mean? 80 00:03:38.220 --> 00:03:41.160 Well, it means that when we call this function, 81 00:03:41.160 --> 00:03:46.160 as we do here, now we must pass in some data 82 00:03:47.080 --> 00:03:48.903 such as this dictionary, 83 00:03:51.690 --> 00:03:54.030 and the value of this dictionary 84 00:03:54.030 --> 00:03:57.330 will go into this which is a new variable 85 00:03:57.330 --> 00:04:00.863 that we are creating when we start running the function. 86 00:04:01.790 --> 00:04:03.470 So all I'm doing here is I'm calling 87 00:04:03.470 --> 00:04:08.470 the calculate_mpg function, but now I am passing in a value. 88 00:04:08.550 --> 00:04:11.860 I'm giving it what's called an argument. 89 00:04:11.860 --> 00:04:15.900 And this argument here is what gives 90 00:04:15.900 --> 00:04:18.630 a value to this parameter. 91 00:04:18.630 --> 00:04:21.580 So the value you pass into a function is called an argument. 92 00:04:21.580 --> 00:04:24.623 The variable that receives the value is called a parameter. 93 00:04:25.930 --> 00:04:28.420 So, if we run this right now, 94 00:04:28.420 --> 00:04:30.663 we can delete this variable by the way, 95 00:04:31.800 --> 00:04:35.170 the exact same thing will happen because now we are running 96 00:04:35.170 --> 00:04:39.460 this function, and we have this car to calculate variable. 97 00:04:39.460 --> 00:04:41.740 And we we run the function, we are passing 98 00:04:41.740 --> 00:04:43.890 this dictionary into it. 99 00:04:43.890 --> 00:04:47.590 So when the variable accesses the mileage property 100 00:04:47.590 --> 00:04:52.000 of this variable, we're accessing this thing here. 101 00:04:52.000 --> 00:04:53.530 And when we access the fuel_consumed, 102 00:04:53.530 --> 00:04:55.163 we're accessing this thing here. 103 00:04:56.900 --> 00:04:59.140 This is all well and good, 104 00:04:59.140 --> 00:05:01.340 but we are still limited by one thing, 105 00:05:01.340 --> 00:05:03.410 which is that we've got this single variable 106 00:05:03.410 --> 00:05:05.580 that we can use to call that function. 107 00:05:05.580 --> 00:05:07.840 So let's make this programme a little bit better. 108 00:05:07.840 --> 00:05:11.200 Instead of passing the dictionary in here, 109 00:05:11.200 --> 00:05:15.140 we are going to create a list of cars. 110 00:05:15.140 --> 00:05:17.700 So here we have a list of cars. 111 00:05:17.700 --> 00:05:20.110 We've got the first car which is the Ford Fiesta 112 00:05:20.110 --> 00:05:21.550 that we were using earlier, 113 00:05:21.550 --> 00:05:23.980 and then we have another one, a Ford Focus, 114 00:05:23.980 --> 00:05:26.363 a Mazda MX-5, and a Mini Cooper. 115 00:05:27.270 --> 00:05:29.360 Now, we have four cars in here. 116 00:05:29.360 --> 00:05:32.593 So we cannot do this. 117 00:05:33.500 --> 00:05:35.660 The reason we can't do this is because if we put 118 00:05:35.660 --> 00:05:39.420 the cars list inside the brackets, 119 00:05:39.420 --> 00:05:43.450 the value of the car's variable, which is this list, 120 00:05:43.450 --> 00:05:46.560 will be assigned to this parameter. 121 00:05:46.560 --> 00:05:49.890 And then, when we try to access the mileage property, 122 00:05:49.890 --> 00:05:53.540 it was trying to access the mileage property of this list. 123 00:05:53.540 --> 00:05:58.270 And again, lists don't have this sort of access. 124 00:05:58.270 --> 00:06:00.740 You would have to access element zero 125 00:06:00.740 --> 00:06:04.140 or something like that if you wanted to access a list. 126 00:06:04.140 --> 00:06:07.200 This is only applicable to dictionaries, as we know, 127 00:06:07.200 --> 00:06:11.240 so we have to access one individual car of this list. 128 00:06:11.240 --> 00:06:15.020 So we can calculate the mpg for cars[0], say, 129 00:06:15.020 --> 00:06:18.360 and that will, once again, give us the Ford Fiesta. 130 00:06:18.360 --> 00:06:20.270 If we change this to cars[1], 131 00:06:20.270 --> 00:06:22.650 that is going to give us the Ford Focus. 132 00:06:22.650 --> 00:06:25.910 If we change it to cars[2], it will give us the MX-5. 133 00:06:25.910 --> 00:06:29.200 So, this is the perfect place to use a for loop. 134 00:06:29.200 --> 00:06:32.293 If we make a for loop for car in cars, 135 00:06:33.620 --> 00:06:36.530 then instead of passing cars zero, one, or two, 136 00:06:36.530 --> 00:06:38.180 we can just pass car. 137 00:06:38.180 --> 00:06:41.270 And that is going to print out one line for each car 138 00:06:41.270 --> 00:06:42.760 as you can see there. 139 00:06:42.760 --> 00:06:45.503 Something important to know is that this variable, 140 00:06:45.503 --> 00:06:50.503 car_to_calculate, can just be renamed to car. 141 00:06:50.920 --> 00:06:53.853 And I appreciate this will be a little bit confusing, 142 00:06:57.270 --> 00:07:01.140 but here's what Python does when it encounters a variable. 143 00:07:01.140 --> 00:07:03.730 Here we're using the car variable. 144 00:07:03.730 --> 00:07:06.010 So the first thing Python is gonna do is it's going to check 145 00:07:06.010 --> 00:07:09.030 whether it was declared in this body. 146 00:07:09.030 --> 00:07:12.200 And in this case, it was because it is the parameter. 147 00:07:12.200 --> 00:07:16.943 So because car was declared here, it will use this one. 148 00:07:18.210 --> 00:07:21.573 If we have another car variable declared up here, 149 00:07:22.970 --> 00:07:26.790 then this one will still be this parameter. 150 00:07:26.790 --> 00:07:29.150 It will not use this one here 151 00:07:29.150 --> 00:07:32.290 unless there is no variable defined there, 152 00:07:32.290 --> 00:07:34.160 then Python will check here. 153 00:07:34.160 --> 00:07:37.480 It won't be there, and it will go to the next level. 154 00:07:37.480 --> 00:07:40.180 So, your variables can take the same name 155 00:07:40.180 --> 00:07:44.670 as something that's declared above if you want. 156 00:07:44.670 --> 00:07:47.470 It's generally discouraged to do that. 157 00:07:47.470 --> 00:07:49.550 So, do it with a bit of caution. 158 00:07:49.550 --> 00:07:51.920 Here I'm doing it because this car variable 159 00:07:51.920 --> 00:07:54.110 that we're declaring inside this for loop 160 00:07:54.110 --> 00:07:56.940 is used down here as a sort of temporary measure. 161 00:07:56.940 --> 00:07:59.280 So I'm okay with calling this variable car. 162 00:07:59.280 --> 00:08:01.600 But sometimes if you have global variables 163 00:08:01.600 --> 00:08:04.670 that you want to keep as unique names, 164 00:08:04.670 --> 00:08:05.840 you can do that, and you can call 165 00:08:05.840 --> 00:08:08.580 these variables something else, such as car_to_calculate. 166 00:08:08.580 --> 00:08:10.410 It's really your choice. 167 00:08:10.410 --> 00:08:11.870 In this case, though, calling it car 168 00:08:11.870 --> 00:08:13.410 makes the code a little bit simpler 169 00:08:13.410 --> 00:08:15.800 because the variable name was a bit long. 170 00:08:15.800 --> 00:08:18.850 So this is how arguments and parameters work. 171 00:08:18.850 --> 00:08:21.440 We've learned how to pass an argument, 172 00:08:21.440 --> 00:08:25.200 which is passing a value and putting it inside the brackets. 173 00:08:25.200 --> 00:08:27.620 And that the value of this variable 174 00:08:27.620 --> 00:08:31.200 will be assigned to this parameter. 175 00:08:31.200 --> 00:08:34.230 It can have the same name, and that's totally fine. 176 00:08:34.230 --> 00:08:37.330 And if you want to have multiple parameters, 177 00:08:37.330 --> 00:08:41.570 such as something like car and intro, say, 178 00:08:41.570 --> 00:08:45.770 such that you can print the intro there, 179 00:08:45.770 --> 00:08:47.880 then you define them like this. 180 00:08:47.880 --> 00:08:51.020 You put parameter one name first, 181 00:08:51.020 --> 00:08:52.800 and that is going to be a new variable 182 00:08:52.800 --> 00:08:55.620 that gets created when the function gets called. 183 00:08:55.620 --> 00:08:58.100 And then a comma and then another variable 184 00:08:58.100 --> 00:09:00.430 that will be created when the function gets called. 185 00:09:00.430 --> 00:09:04.670 Now, this function needs two arguments 186 00:09:04.670 --> 00:09:06.740 because it has two parameters. 187 00:09:06.740 --> 00:09:11.740 So down here, you can pass in after the comma 188 00:09:12.310 --> 00:09:15.550 and entirely new argument that will now 189 00:09:15.550 --> 00:09:17.450 be the value for intro. 190 00:09:17.450 --> 00:09:20.751 So this will be the value of that, 191 00:09:20.751 --> 00:09:23.570 and this will be the value of that. 192 00:09:23.570 --> 00:09:25.800 If we run this, you'll see that every car 193 00:09:25.800 --> 00:09:30.373 prints New car in front because that's the value of intro. 194 00:09:31.520 --> 00:09:32.780 Hopefully this makes sense. 195 00:09:32.780 --> 00:09:34.530 I hope I haven't confused you too much. 196 00:09:34.530 --> 00:09:38.350 Functions are going to come in really handy as we develop, 197 00:09:38.350 --> 00:09:41.540 and indeed, whenever we're making longer pieces of code, 198 00:09:41.540 --> 00:09:44.680 it's often a good idea to split different parts of them 199 00:09:44.680 --> 00:09:47.810 into functions so that the code is a bit more readable. 200 00:09:47.810 --> 00:09:49.840 Something very important about functions 201 00:09:49.840 --> 00:09:53.390 is giving them a name that describes what they do 202 00:09:53.390 --> 00:09:56.640 so that when you read the code that uses the function 203 00:09:56.640 --> 00:09:59.640 you can very easily tell what it's doing. 204 00:09:59.640 --> 00:10:01.550 If you're not sure what to call a function, 205 00:10:01.550 --> 00:10:04.350 don't worry, it's the same thing as naming a variable. 206 00:10:04.350 --> 00:10:07.620 It takes time and experience to get really good at it. 207 00:10:07.620 --> 00:10:09.100 Thanks for joining me in this video. 208 00:10:09.100 --> 00:10:10.110 Hope you've learned something, 209 00:10:10.110 --> 00:10:11.760 and I'll see you in the next one.